Skip to content

Commit

Permalink
Moonpig::UserAgent learns how to return a useful result on error inst…
Browse files Browse the repository at this point in the history
…ead of dying

It's controlled by a `die_on_error` attribute
  • Loading branch information
mjdominus committed Feb 23, 2024
1 parent 2e00b48 commit f083c57
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/Moonpig/UserAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ has JSON => (
handles => [ qw(encode decode) ],
);

has die_on_error => (
is => 'rw',
isa => 'Bool',
default => 1,
);

sub mp_time {
my ($self) = @_;
my $time = $self->mp_request(GET => '/time')->{now};
Expand Down Expand Up @@ -86,10 +92,20 @@ sub mp_request {

# eventually there should be exceptions here for all 2xx codes
unless ($res->code == 200) {
my $error = sprintf "unexpected response from Moonpig:\n"
. "request : %s %s\n"
. "response: \n%s", uc $method, $target, $res->as_string;
die $error;
if ($self->die_on_error) {
my $error = sprintf "unexpected response from Moonpig:\n"
. "request : %s %s\n"
. "response: \n%s", uc $method, $target, $res->as_string;
die $error;
} else {
return {
error => 1,
code => $res->code,
method => uc $method,
target => $target,
response => $res->as_string,
};
}
}

${ $extra_arg->{response} } = $res if $extra_arg->{response};
Expand Down

0 comments on commit f083c57

Please sign in to comment.