mirror of
git://git.proxmox.com/git/pve-apiclient.git
synced 2025-01-09 01:18:05 +03:00
use new Exception.pm class to signal errors to caller
Allows a caller to acces the HTTP response code, which may be useful to handle application logic. E.g., catching a HTTP_NOT_IMPLEMENTED and fallback to an older method. Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
6700b1517e
commit
097484f42d
@ -11,6 +11,7 @@ use JSON;
|
|||||||
use Data::Dumper; # fixme: remove
|
use Data::Dumper; # fixme: remove
|
||||||
use HTTP::Request::Common;
|
use HTTP::Request::Common;
|
||||||
use Carp;
|
use Carp;
|
||||||
|
use PVE::APIClient::Exception qw(raise);
|
||||||
|
|
||||||
my $extract_data = sub {
|
my $extract_data = sub {
|
||||||
my ($res) = @_;
|
my ($res) = @_;
|
||||||
@ -121,7 +122,7 @@ sub login {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$response->is_success) {
|
if (!$response->is_success) {
|
||||||
die $response->status_line . "\n";
|
raise($response->status_line ."\n", code => $response->code)
|
||||||
}
|
}
|
||||||
|
|
||||||
my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
||||||
@ -210,7 +211,7 @@ sub call {
|
|||||||
} elsif ($method eq 'DELETE') {
|
} elsif ($method eq 'DELETE') {
|
||||||
$response = $ua->request(HTTP::Request::Common::DELETE($uri));
|
$response = $ua->request(HTTP::Request::Common::DELETE($uri));
|
||||||
} else {
|
} else {
|
||||||
die "method $method not implemented\n";
|
raise("method $method not implemented\n");
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
};
|
};
|
||||||
@ -229,27 +230,21 @@ sub call {
|
|||||||
|
|
||||||
if ($response->is_success) {
|
if ($response->is_success) {
|
||||||
|
|
||||||
die "got unexpected content type" if $ct !~ m|application/json|;
|
raise("got unexpected content type", code => $response->code)
|
||||||
|
if $ct !~ m|application/json|;
|
||||||
|
|
||||||
return from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
return from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
my $msg = $response->status_line . "\n";
|
my $msg = $response->message;
|
||||||
eval {
|
my $errors = eval {
|
||||||
return if $ct !~ m|application/json|;
|
return if $ct !~ m|application/json|;
|
||||||
my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
|
||||||
if (my $errors = $res->{errors}) {
|
return $res->{errors};
|
||||||
foreach my $key (keys %$errors) {
|
|
||||||
my $m = $errors->{$key};
|
|
||||||
chomp($m);
|
|
||||||
$m =~s/\n/ -- /g;
|
|
||||||
$msg .= " $key: $m\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
die $msg;
|
|
||||||
|
|
||||||
|
raise("$msg\n", code => $response->code, errors => $errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user