| 1 | package WebAPI::DBIC::Resource::Role::DBICException; | ||||||||||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::DBICException::VERSION = '0.004002'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use Devel::Dwarn; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use JSON::MaybeXS qw(JSON); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 25 | requires 'response'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 153 | ▶ | sub finish_request { | ||||||||||||||||||||||||||
|
Subroutine
finish_request: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 153 | my ($self, $metadata) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 153 | return $self->handle_web_machine_exception($metadata->{exception}); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # XXX we probably ought to allow a stck/list of handlers that can try to | ||||||||||||||||||||||||||||
| 23 | # recognise an exception - we'd try them in turn, perhaps until one has | ||||||||||||||||||||||||||||
| 24 | # converted it into an object that has an as_psgi method. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 153 | ▶ | sub handle_web_machine_exception { | ||||||||||||||||||||||||||
|
Subroutine
handle_web_machine_exception: called CC=11 SCAR=31.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 153 | my ($self, $exception) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 153 | ▶ | return unless $exception; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | #warn "$exception"; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 10 | ▶ | if (blessed($exception) && $exception->can('as_psgi')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($exception) and $exception->can("as_psgi")
MC/DC50%1/2
builtin::blessed($exception) and $exception->can("as_psgi")
builtin::blessed($exception)
$exception->can("as_psgi")
Truth table66%2/3
A and B
A= builtin::blessed($exception)B= $exception->can("as_psgi")
| |||||||||||||||||||||||||||||
| 34 | 9 | my ($status, $headers, $body) = @{ $exception->as_psgi }; | |||||||||||||||||||||||||||
| 35 | 9 | $self->response->status($status); | |||||||||||||||||||||||||||
| 36 | 9 | $self->response->headers($headers); | |||||||||||||||||||||||||||
| 37 | 9 | $self->response->body($body); | |||||||||||||||||||||||||||
| 38 | 9 | return; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | #$exception->rethrow if ref $exception and $exception->can('rethrow'); | ||||||||||||||||||||||||||||
| 42 | #die $exception if ref $exception; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | (my $line1 = $exception) =~ s/\n.*//ms; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my $error_data; | |||||||||||||||||||||||||||
| 47 | # ... DBD::Pg::st execute failed: ERROR: column "nonesuch" does not exist | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if ($exception =~ m/DBD::.*? \s+ failed:.*? \s+ column:? \s+ "?(.*?)"? \s+ (.*)/x) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 0 | $error_data = { | |||||||||||||||||||||||||||
| 50 | status => 400, | ||||||||||||||||||||||||||||
| 51 | field => $1, | ||||||||||||||||||||||||||||
| 52 | foo => "$1: $2", | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | # handle exceptions from Params::Validate | ||||||||||||||||||||||||||||
| 56 | elsif ($exception =~ /The \s '(\w+)' \s parameter \s \(.*?\) \s to \s (\S+) \s did \s not \s pass/x) { | ||||||||||||||||||||||||||||
| 57 | 0 | $error_data = { | |||||||||||||||||||||||||||
| 58 | status => 400, | ||||||||||||||||||||||||||||
| 59 | field => $1, | ||||||||||||||||||||||||||||
| 60 | message => $line1, | ||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | warn "Exception: $line1 (@{[ %{ $error_data||{} } ]})\n" | ||||||||||||||||||||||||||
|
Condition0%0/2
$error_data || {}
MC/DC0%0/1
$error_data || {}
$error_data
Truth table0%0/2
A
A= $error_data
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($error_data) { # we recognized the exception | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $error_data->{status} ||= 500; | ||||||||||||||||||||||||||
|
Condition0%0/2
$error_data->{"status"} ||= 500
MC/DC0%0/1
$error_data->{"status"} ||= 500
$error_data->{"status"}
Truth table0%0/2
A
A= $error_data->{"status"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # only include detailed exception information if not in production | ||||||||||||||||||||||||||||
| 72 | # (as it might contain sensitive information) | ||||||||||||||||||||||||||||
| 73 | $error_data->{_embedded}{exceptions}[0]{exception} = "$exception" # stringify | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if $ENV{PLACK_ENV} ne 'production'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # create response | ||||||||||||||||||||||||||||
| 77 | # XXX would be nice to create an exception object that can as_psgi() | ||||||||||||||||||||||||||||
| 78 | # then reuse the handling of that above | ||||||||||||||||||||||||||||
| 79 | # XXX would also be good to adopt a more formal error structure, such as | ||||||||||||||||||||||||||||
| 80 | # application/vnd.error+json => https://github.com/blongden/vnd.error | ||||||||||||||||||||||||||||
| 81 | 0 | my $json = JSON->new->ascii->pretty; | |||||||||||||||||||||||||||
| 82 | 0 | my $response = $self->response; | |||||||||||||||||||||||||||
| 83 | 0 | $response->status($error_data->{status}); | |||||||||||||||||||||||||||
| 84 | 0 | my $body = $json->encode($error_data); | |||||||||||||||||||||||||||
| 85 | 0 | $response->body($body); | |||||||||||||||||||||||||||
| 86 | 0 | $response->content_length(length $body); | |||||||||||||||||||||||||||
| 87 | 0 | $response->content_type('application/json'); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 1 | warn "Exception: $line1\n" | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 25 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||