PONAPI/Exception.pm

filter: Statementstmt 95.5% 65 / 68 Branchbran 85.0% 17 / 20 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 14 / 14 Podpod 50.0% 2 / 4 totaltotal 87.3% 110 / 126 CC 15 SCAR 13.8
CC
15
Cov
90%
_handle_exception_obj
8.0
new_from_exception
5.0
as_response
4.0
CRAP 4.0
« Test/PONAPI/Repository/MockDB/Table.pm ↑ Summary PONAPI/DAO/Request/RetrieveByRelationship.pm »
1 # ABSTRACT: Exceptions for PONAPI::Server
2 package PONAPI::Exception;
3  
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Moose::Util qw/find_meta/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub throw {
10 20 ▶ my $class_or_obj = shift;
Subroutine throw: called CC=2 SCAR=7.2
Pod undocumented
11 20 ▶ die ( blessed $class_or_obj ? $class_or_obj : $class_or_obj->new(@_) );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($class_or_obj) ? : 0 20 20
12 }
13  
14 use overload
15 8 q{""} => 'as_string',
16 8 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 has message => (
19 is => 'ro',
20 isa => 'Str',
21 required => 1,
22 );
23  
24 has status => (
25 is => 'ro',
26 isa => 'Int',
27 17 ▶ default => sub { 400 },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 );
29  
30 8 has bad_request_data => (
31 is => 'ro',
32 isa => 'Bool',
33 );
34  
35 8 has sql_error => (
36 is => 'ro',
37 isa => 'Bool',
38 );
39  
40 8 has internal => (
41 is => 'ro',
42 isa => 'Bool',
43 );
44  
45 has json_api_version => (
46 is => 'ro',
47 isa => 'Str',
48 48 ▶ default => sub { '1.0' },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 8 writer => '_set_json_api_version'
50 );
51  
52 # Picked from Throwable::Error
53 sub as_string {
54 52 ▶ my $self = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
55 52 return $self->message;
56 }
57  
58 sub as_response {
59 48 ▶ my $self = shift;
Subroutine as_response: called CC=4 SCAR=13.9
Pod documented
60  
61 48 my $status = $self->status;
62 48 my $detail = $self->message;
63  
64 48 ▶ if ( $self->sql_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->sql_error) { } 3 45 48
elsif ($self->bad_request_data) { } 7 38 45
65 3 $detail = "SQL error: $detail";
66 }
67 elsif ( $self->bad_request_data ) {
68 7 $detail = "Bad request data: $detail";
69 }
70 else {
71 38 $status = 500;
72 38 ▶ warn $detail if $detail;
Branch: 1 branch
BranchTrueFalseTotal
if $detail 14 24 38
73 38 $detail = "A fatal error has occured, please check server logs";
74 }
75  
76 48 return $status, [], +{
77 jsonapi => { version => $self->json_api_version },
78 errors => [ { detail => $detail, status => $status } ],
79 };
80 }
81  
82 sub new_from_exception {
83 47 ▶ my ( $class, $e ) = @_;
Subroutine new_from_exception: called CC=5 SCAR=16.1
Pod undocumented
84  
85 47 ▶ return $e if blessed($e) && $e->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($e) and $e->isa($class) 19 28 47
Condition100%3/3
builtin::blessed($e) and $e->isa($class)
!ll&&!rl&&r
24419
MC/DC100%2/2
builtin::blessed($e) and $e->isa($class)
builtin::blessed($e) $e->isa($class)
Truth table100%3/3
A and B
A= builtin::blessed($e)B= $e->isa($class)
ABresult
0X0
100
111
86  
87 28 my %args_for_new = $class->_handle_exception_obj($e);
88  
89 28 ▶ unless ( $args_for_new{status} and $args_for_new{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args_for_new{"status"} and $args_for_new{"message"}) 24 4 28
Condition66%2/3
$args_for_new{"status"} and $args_for_new{"message"}
!ll&&!rl&&r
2404
MC/DC50%1/2
$args_for_new{"status"} and $args_for_new{"message"}
$args_for_new{"status"} $args_for_new{"message"}
Truth table66%2/3
A and B
A= $args_for_new{"status"}B= $args_for_new{"message"}
ABresult
0X0
100
111
90 24 %args_for_new = (
91 status => 500,
92 message => '',
93 );
94 24 warn "$e";
95 }
96  
97 28 return $class->new(%args_for_new);
98 }
99  
100 sub _handle_exception_obj {
101 28 ▶ my ( $self, $e ) = @_;
Subroutine _handle_exception_obj: called CC=7 SCAR=20.8
102 28 ▶ return unless blessed($e) or $e->isa('Moose::Exception');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($e) or $e->isa("Moose::Exception") 24 4 28
Condition66%2/3
builtin::blessed($e) or $e->isa("Moose::Exception")
l!l&&r!l&&!r
4024
MC/DC50%1/2
builtin::blessed($e) or $e->isa("Moose::Exception")
builtin::blessed($e) $e->isa("Moose::Exception")
Truth table66%2/3
A or B
A= builtin::blessed($e)B= $e->isa("Moose::Exception")
ABresult
1X1
011
000
103  
104 4 ▶ if ( $e->isa('Moose::Exception::AttributeIsRequired') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->isa('Moose::Exception::AttributeIsRequired')) { } 2 2 4
elsif ($e->isa('Moose::Exception::ValidationFailedForTypeConstraint') or $e->isa('Moose::Exception::ValidationFailedForInlineTypeConstraint')) { } 2 0 2
105 2 my $attribute = $e->attribute_name;
106 2 return _bad_req( "Parameter `$attribute` is required" );
107 }
108 ▶ elsif (
Condition33%1/3
$e->isa("Moose::Exception::ValidationFailedForTypeConstraint") or $e->isa("Moose::Exception::ValidationFailedForInlineTypeConstraint")
l!l&&r!l&&!r
020
MC/DC0%0/2
$e->isa("Moose::Exception::ValidationFailedForTypeConstraint") or $e->isa("Moose::Exception::ValidationFailedForInlineTypeConstraint")
$e->isa("Moose::Exception::ValidationFailedForTypeConstraint") $e->isa("Moose::Exception::ValidationFailedForInlineTypeConstraint")
Truth table33%1/3
A or B
A= $e->isa("Moose::Exception::ValidationFailedForTypeConstraint")B= $e->isa("Moose::Exception::ValidationFailedForInlineTypeConstraint")
ABresult
1X1
011
000
109 $e->isa('Moose::Exception::ValidationFailedForTypeConstraint') or
110 $e->isa('Moose::Exception::ValidationFailedForInlineTypeConstraint')
111 ) {
112 2 my $class = find_meta( $e->class_name );
113 2 my $attribute = $class->get_attribute( $e->attribute_name );
114 2 my $value_nice = JSON::MaybeXS->new->allow_nonref->utf8->canonical->encode( $e->value );
115  
116 2 ▶ if ( !$attribute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribute) 0 2 2
117 0 my $attr = $e->attribute_name;
118 0 return _bad_req( "Parameter `$attr` got an expected data type: $value_nice" );
119 }
120  
121 2 my $attribute_name = $attribute->name;
122 2 my $type_name = _moose_type_to_nice_description( $attribute->{isa} );
123  
124 2 return _bad_req( "Parameter `$attribute_name` expected $type_name, but got a $value_nice" );
125 }
126  
127 0 return;
128 }
129  
130 sub _bad_req {
131 return (
132 4 ▶ message => shift,
Subroutine _bad_req: called CC=1 SCAR=0.0
133 status => 400,
134 bad_request_data => 1,
135 );
136 }
137  
138 # THIS IS NOT COMPLETE, NOR IS IT MEANT TO BE
139 sub _moose_type_to_nice_description {
140 2 ▶ my ($type_name) = @_;
Subroutine _moose_type_to_nice_description: called CC=1 SCAR=0.0
141  
142 2 $type_name =~ s/ArrayRef/Collection/g;
143 2 $type_name =~ s/HashRef/Resource/g;
144 2 $type_name =~ s/Maybe\[(.+)]/null or $1/g;
145 2 $type_name =~ s/\|/ or /g;
146  
147 2 return $type_name;
148 }
149  
150 8 __PACKAGE__->meta->make_immutable;
151 8 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 __END__
154  
155 =pod
156  
157 =encoding UTF-8
158  
159 =head1 NAME
160  
161 PONAPI::Exception - Exceptions for PONAPI::Server
162  
163 =head1 VERSION
164  
165 version 0.003002
166  
167 =head1 SYNOPSIS
168  
169 use PONAPI::Exception;
170 PONAPI::Exception->throw( message => "Generic exception" );
171 PONAPI::Exception->throw(
172 message => "Explanation for the sql error, maybe $DBI::errstr",
173 sql => 1,
174 );
175 PONAPI::Exception->throw(
176 message => "Data had type `foo` but we wanted `bar`",
177 bad_request_data => 1,
178 );
179  
180 =head1 DESCRIPTION
181  
182 I<PONAPI::Exception> can be used by repositories to signal errors;
183 exceptions thrown this way will be caught by L<the DAO|PONAPI::DAO> and
184 handled gracefully.
185  
186 Different kinds of exceptions can be thrown by changing the arguments
187 to C<throw>; C<sql =E<gt> 1> will throw a SQL exception,
188 C<bad_request_data =E<gt> 1> will throw an exception due to the
189 input data being wrong, and not passing any of those will
190 throw a generic exception.
191  
192 The human-readable C<message> for all of those will end up in the
193 error response returned to the user.
194  
195 =head1 METHODS
196  
197 =head2 message
198  
199 This attribute contains the exception message.
200  
201 =head2 as_string
202  
203 Returns a stringified form of the exception. The object is overloaded
204 to return this if used in string context.
205  
206 =head2 as_response
207  
208 Returns the exception as a 3-element list that may be fed directly
209 to plack as a {json:api} response.
210  
211 $e->as_response; # ( $status, [], { errors => [ { detail => $message } ] } )
212  
213 =head2 json_api_version
214  
215 Defaults to 1.0; only used in C<as_response>.
216  
217 =head2 status
218  
219 HTTP Status code for the exception; in most cases you don't need to
220 set this manually.
221  
222 =head1 AUTHORS
223  
224 =over 4
225  
226 =item *
227  
228 Mickey Nasriachi <mickey@cpan.org>
229  
230 =item *
231  
232 Stevan Little <stevan@cpan.org>
233  
234 =item *
235  
236 Brian Fraser <hugmeir@cpan.org>
237  
238 =back
239  
240 =head1 COPYRIGHT AND LICENSE
241  
242 This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser.
243  
244 This is free software; you can redistribute it and/or modify it under
245 the same terms as the Perl 5 programming language system itself.
246  
247 =cut
« Test/PONAPI/Repository/MockDB/Table.pm ↑ Summary PONAPI/DAO/Request/RetrieveByRelationship.pm »