Commit 2b57e898 authored by Côme Chilliet's avatar Côme Chilliet

feat(WorkflowUpdate) Improve REST API error handling

issue #5745
parent c41e59a9
......@@ -35,7 +35,7 @@ use JSON;
use Argonaut::Libraries::Common qw(:config);
use Exporter 'import'; # gives you Exporter's import() method directly
our @EXPORT_OK = qw(&argonaut_get_rest_client); # symbols to export on request
our @EXPORT_OK = qw(&argonaut_get_rest_client &argonaut_parse_rest_error); # symbols to export on request
=item argonaut_get_rest_client
Get REST client connection using information from configuration file
......@@ -72,12 +72,37 @@ sub argonaut_get_rest_client {
my $token = JSON->new->utf8->allow_nonref->decode($client->responseContent());
$client->addHeader('Session-Token', $token);
} else {
die('Connection to REST API failed: '.$client->responseCode().' '.$client->responseContent());
die('Connection to REST API failed: '.$client->responseCode().' - '.argonaut_parse_rest_error($client)."\n");
}
return $client;
}
=item argonaut_parse_rest_error
Parse REST response after an error and returns string version of the first error
=cut
sub argonaut_parse_rest_error {
my ($client) = @_;
my $error = JSON->new->utf8->allow_nonref->decode($client->responseContent());
if (ref($error) eq "ARRAY") {
$error = $error->[0];
}
my $errorMessage = $error;
if (ref($error) eq "HASH") {
$errorMessage = $error->{'message'};
if (defined $error->{'file'}) {
$errorMessage .= ' ('.$error->{'file'};
if (defined $error->{'line'}) {
$errorMessage .= ':'.$error->{'line'};
}
$errorMessage .= ')';
}
}
return $errorMessage;
}
1;
__END__
......@@ -105,12 +105,12 @@ sub argonaut_supann_update_states {
if ($updateNeeded) {
$client->PUT('/objects/user/'.$dn.'/supannAccountStatus/supannRessourceEtatDate', encode_json($supannRessourceEtatDateNewValues));
if ($client->responseCode() ne '200') {
die('Request to REST API failed: '.$client->responseCode().' '.$client->responseContent());
die('Request to REST API failed: '.$client->responseCode().' - '.argonaut_parse_rest_error($client));
}
}
}
} else {
die('Request to REST API failed: '.$client->responseCode().' '.$client->responseContent());
die('Request to REST API failed: '.$client->responseCode().' - '.argonaut_parse_rest_error($client));
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment