From db2f12942de088e53347df4b255dde472f00561b Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 19 Sep 2024 13:39:07 +0000 Subject: [PATCH] Bug 37963: Case 1 - Return if sushi_errors even if response code >= 400 This ensures that SUSHI errors, if present, are shown on the UI even if response->code >= 400. This means that if, for example, SUSHI provider returns 401 with SUSHI error like so: {"Code": 2020, "Severity": "Error", "Message": "API Key Invalid"} Then, the 'API Key Invalid' will be shown to the user, without having to check logs. Signed-off-by: David Nind --- Koha/ERM/EUsage/UsageDataProvider.pm | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Koha/ERM/EUsage/UsageDataProvider.pm b/Koha/ERM/EUsage/UsageDataProvider.pm index e85ee58ce8..716610052d 100644 --- a/Koha/ERM/EUsage/UsageDataProvider.pm +++ b/Koha/ERM/EUsage/UsageDataProvider.pm @@ -180,8 +180,11 @@ sub harvest_sushi { $ua->agent( 'Koha/' . Koha::version() ); my $response = $ua->simple_request($request); + my $result = decode_json( $response->decoded_content ); + if ( $response->code >= 400 ) { - my $result = decode_json( $response->decoded_content ); + + return if $self->_sushi_errors($result); my $message; if ( ref($result) eq 'ARRAY' ) { @@ -199,7 +202,6 @@ sub harvest_sushi { } } - #TODO: May want to add a job error message here? warn sprintf "ERROR - SUSHI service %s returned %s - %s\n", $url, $response->code, $message; if ( $response->code == 404 ) { @@ -208,7 +210,6 @@ sub harvest_sushi { Koha::Exceptions::Authorization::Unauthorized->throw($message); } else { - #TODO: May want to add a job error message here? die sprintf "ERROR requesting SUSHI service\n%s\ncode %s: %s\n", $url, $response->code, $message; @@ -217,12 +218,10 @@ sub harvest_sushi { return; } - my $decoded_response = decode_json( $response->decoded_content ); - - return if $self->_sushi_errors($decoded_response); + return if $self->_sushi_errors($result); # Parse the SUSHI response - my $sushi_counter = Koha::ERM::EUsage::SushiCounter->new( { response => $decoded_response } ); + my $sushi_counter = Koha::ERM::EUsage::SushiCounter->new( { response => $result } ); my $counter_file = $sushi_counter->get_COUNTER_from_SUSHI; return if $self->_counter_file_size_too_large($counter_file); -- 2.39.5