GraphQL/Error.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran 87.5% 14 / 16 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 91.4% 96 / 105 CC 13 SCAR 16.1
CC
13
Cov
93%
_envelope
10.0
__ANON__
4.1
message
2.0
CRAP 5.0
« GraphQL/Mutation.pm ↑ Summary GraphQL/ScalarMap.pm »
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
BranchTrueFalseTotal
if ($want) { } 0 293 293
57 293 else { $ret[0] = $resolve->(@args) }
58 229 1;
59 };
60 293 ▶ return $want ? @ret : $ret[0] if $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $ok 229 64 293
$want ? : 0 229 229
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
BranchTrueFalseTotal
if builtin::blessed($error) and $error->isa(__PACKAGE__) || $error->isa("GraphQL::Error") 54 10 64
Condition83%5/6
builtin::blessed($error) and $error->isa(__PACKAGE__) || $error->isa("GraphQL::Error")
!ll&&!rl&&r
1954
$error->isa(__PACKAGE__) or $error->isa("GraphQL::Error")
l!l&&r!l&&!r
5409
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")
ABCresult
0XX0
1000
11X1
1011
68 && ($error->isa(__PACKAGE__) || $error->isa('GraphQL::Error'));
69  
70 10 ▶ if (Scalar::Util::blessed($error) && $error->isa('DBIO::Exception::Server')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa("DBIO::Exception::Server")) 9 1 10
Condition66%2/3
builtin::blessed($error) and $error->isa("DBIO::Exception::Server")
!ll&&!rl&&r
109
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")
ABresult
0X0
100
111
71 9 ▶ my $hit = $CLASSIFIED{ $error->sqlstate // '' };
Condition100%2/2
$error->sqlstate // ""
l!l
54
MC/DC100%1/1
$error->sqlstate // ""
$error->sqlstate
Truth table100%2/2
A
A= $error->sqlstate
Aresult
11
00
72 9 ▶ return GraphQL::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
if $hit 4 5 9
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
BranchTrueFalseTotal
if ($on_error) { } 4 2 6
80 4 ▶ eval { $on_error->($error, $id); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$on_error($error, $id); 1 } } or warn "DBIO::GraphQL: on_error hook failed (correlation id $id)\n" 3 1 4
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__
« GraphQL/Mutation.pm ↑ Summary GraphQL/ScalarMap.pm »