add try/catch for error handling

add try/catch for error handling
must installed Try::Tiny package

[Ticket: no]
This commit is contained in:
Marko Oldenburg 2022-02-02 14:31:46 +01:00
parent cd4015330e
commit 302ad14dda
3 changed files with 41 additions and 12 deletions

View File

@ -330,7 +330,7 @@ use FHEM::Meta;
], ],
"release_status": "stable", "release_status": "stable",
"license": "GPL_2", "license": "GPL_2",
"version": "v3.4.2", "version": "v3.6.0",
"author": [ "author": [
"Marko Oldenburg <fhemdevelopment@cooltux.net>" "Marko Oldenburg <fhemdevelopment@cooltux.net>"
], ],

View File

@ -1,2 +1,2 @@
UPD 2022-02-02_12:25:53 18011 FHEM/82_LGTV_WebOS.pm UPD 2022-02-02_14:30:51 18011 FHEM/82_LGTV_WebOS.pm
UPD 2022-02-02_13:49:22 54363 lib/FHEM/Devices/LGTV/LGTVWebOS.pm UPD 2022-02-02_14:30:52 55048 lib/FHEM/Devices/LGTV/LGTVWebOS.pm

View File

@ -34,6 +34,13 @@ use strict;
use warnings; use warnings;
use experimental qw /switch/; use experimental qw /switch/;
## try / catch
use Try::Tiny;
# use Carp;
use autodie qw /:io/;
##
use GPUtils qw(GP_Import); use GPUtils qw(GP_Import);
use FHEM::Meta; use FHEM::Meta;
@ -982,12 +989,23 @@ sub ResponseProcessing {
return; return;
} }
my $decode_json = eval { decode_json( encode_utf8($json) ) }; my $decode_json;
if ($@) { try {
Log3( $name, 3, $decode_json = decode_json( encode_utf8($json) );
"LGTV_WebOS ($name) - JSON error while request: $@" );
return;
} }
catch {
if ( $_->isa('autodie::exception') && $_->matches(':io') ) {
Log3( $name, 3,
"LGTV_WebOS ($name) autodie - JSON error while request: $_"
);
return;
}
else {
Log3( $name, 3,
"LGTV_WebOS ($name) - JSON error while request: $_" );
return;
}
}; # Note semicolon.
WriteReadings( $hash, $decode_json ); WriteReadings( $hash, $decode_json );
@ -1418,11 +1436,22 @@ sub CreateSendCommand {
#::Log3( $name, 5, "LGTV_WebOS ($name) - Payload Message: $command->{payload}{message}" ); #::Log3( $name, 5, "LGTV_WebOS ($name) - Payload Message: $command->{payload}{message}" );
my $cmd = eval { encode_json($command) }; my $cmd;
if ($@) { try {
Log3( $name, 3, "LGTV_WebOS ($name) - can't $cmd encode to json: $@" ); $cmd = encode_json($command);
return;
} }
catch {
if ( $_->isa('autodie::exception') && $_->matches(':io') ) {
Log3( $name, 3,
"LGTV_WebOS ($name) - can't $cmd encode to json: $_" );
return;
}
else {
Log3( $name, 3,
"LGTV_WebOS ($name) - can't $cmd encode to json: $_" );
return;
}
};
::Log3( $name, 5, "LGTV_WebOS ($name) - Sending command: $cmd" ); ::Log3( $name, 5, "LGTV_WebOS ($name) - Sending command: $cmd" );