| 1 | package DBIO::GraphQL::Error; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Client errors of DBIO::GraphQL and the error envelope around resolvers | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 14 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use GraphQL::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use DBIO::Exception::Server; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 14 | our @EXPORT_OK = qw(client_error); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 55 | ▶ | '""' => sub { $_[0]{message} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 109 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 14 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Two kinds of failure leave a resolver: | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # * a client error: DBIO::GraphQL itself words it for the client | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # (validation, bounds, authorization). It is an object of this class and | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # reaches the client as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # * anything else (a database refusal, a Perl error): never worded for the | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # client. The envelope classifies a database refusal by its SQLSTATE and | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # answers everything else with "internal error" and a correlation id; the | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # original goes to the on_error hook only. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 14 | my %CLASSIFIED = ( | |||||||||||||||||||||||||||||||||||||||||
| 31 | '23505' => [ UNIQUE_VIOLATION => 'unique constraint violated' ], | ||||||||||||||||||||||||||||||||||||||||||
| 32 | '23503' => [ FOREIGN_KEY_VIOLATION => 'foreign key constraint violated' ], | ||||||||||||||||||||||||||||||||||||||||||
| 33 | '23502' => [ NOT_NULL_VIOLATION => 'not null constraint violated' ], | ||||||||||||||||||||||||||||||||||||||||||
| 34 | '23514' => [ CHECK_VIOLATION => 'check constraint violated' ], | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 14 | my $counter = 0; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 55 | ▶ | sub client_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 55 | (my $message = join '', @_) =~ s/\n+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 55 | return bless { message => $message }, __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub message { $_[0]{message} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 665 | ▶ | sub guard { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guard: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 665 | my ($class, $resolve, $on_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 293 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 293 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 293 | my @ret; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 293 | my $want = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 293 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($want) { @ret = $resolve->(@args) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 293 | else { $ret[0] = $resolve->(@args) } | |||||||||||||||||||||||||||||||||||||||||
| 58 | 229 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 293 | ▶ | return $want ? @ret : $ret[0] if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 64 | die $class->_envelope($@, $on_error); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 665 | }; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 64 | ▶ | sub _envelope { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_envelope: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 64 | my ($class, $error, $on_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 64 | ▶ | return $error if Scalar::Util::blessed($error) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
builtin::blessed($error) and $error->isa(__PACKAGE__) || $error->isa("GraphQL::Error")
$error->isa(__PACKAGE__) or $error->isa("GraphQL::Error")
MC/DC66%2/3
builtin::blessed($error) and $error->isa(__PACKAGE__) || $error->isa("GraphQL::Error")
builtin::blessed($error)
$error->isa(__PACKAGE__)
$error->isa("GraphQL::Error")
Truth table75%3/4
A and B or C
A= builtin::blessed($error)B= $error->isa(__PACKAGE__)C= $error->isa("GraphQL::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | && ($error->isa(__PACKAGE__) || $error->isa('GraphQL::Error')); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | ▶ | if (Scalar::Util::blessed($error) && $error->isa('DBIO::Exception::Server')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($error) and $error->isa("DBIO::Exception::Server")
MC/DC50%1/2
builtin::blessed($error) and $error->isa("DBIO::Exception::Server")
builtin::blessed($error)
$error->isa("DBIO::Exception::Server")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("DBIO::Exception::Server")
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | ▶ | my $hit = $CLASSIFIED{ $error->sqlstate // '' }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$error->sqlstate // ""
MC/DC100%1/1
$error->sqlstate // ""
$error->sqlstate
Truth table100%2/2
A
A= $error->sqlstate
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | ▶ | return GraphQL::Error->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | message => $hit->[1], | ||||||||||||||||||||||||||||||||||||||||||
| 74 | extensions => { code => $hit->[0] }, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ) if $hit; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 6 | my $id = sprintf '%x-%x-%x-%06x', time, $$, ++$counter, int rand 0xffffff; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | ▶ | if ($on_error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | eval { $on_error->($error, $id); 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | or warn "DBIO::GraphQL: on_error hook failed (correlation id $id)\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | warn "DBIO::GraphQL: internal error, correlation id $id: $error\n"; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | return GraphQL::Error->new( | |||||||||||||||||||||||||||||||||||||||||
| 87 | message => 'internal error', | ||||||||||||||||||||||||||||||||||||||||||
| 88 | extensions => { code => 'INTERNAL_ERROR', correlation_id => $id }, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||||||||||||