Skip to content

Commit

Permalink
Better exception handling in APICaller
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelruiz committed Jan 17, 2013
1 parent 60a234e commit be4039f
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/Opsview/REST/APICaller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use Moo::Role;

use Carp;

use Try::Tiny;
use JSON::XS ();
use HTTP::Tiny;
use HTTP::Tiny 0.014;

has ua => (
is => 'ro',
Expand Down Expand Up @@ -73,15 +74,23 @@ sub put {

sub _errmsg {
my ($self, $r) = @_;
my $cont; $cont = $self->json->decode($r->{content})
if $r->{content};

return Opsview::REST::Exception->new(
status => $r->{status},
reason => $r->{reason},
message => $cont ? $cont->{message} : undef,
detail => $cont ? $cont->{detail} : undef,
);
my $exc = {
status => $r->{status},
reason => $r->{reason},
};

if ($r->{content}) {
try {
my $resp;
$resp = $self->json->decode($r->{content});
$exc->{message} = $resp->{message} || undef;
$exc->{detail} = $resp->{detail} || undef;
} catch {
$exc->{message} = $r->{content};
};
}

return Opsview::REST::Exception->new($exc);
}

1;
Expand Down

0 comments on commit be4039f

Please sign in to comment.