blib/lib/OAuthomatic/Error.pm

filter: Statementstmt 55.5% 55 / 99 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 13 / 26 Podpod 40.0% 2 / 5 totaltotal 44.0% 70 / 159 CC 13 SCAR 18.9
CC
13
Cov
44%
_build_struct_detail
20.0
is_new_client_key_required
12.0
is_new_token_required
12.0
CRAP 6.6
« blib/lib/OAuthomatic/SecretStorage/Keyring.pm ↑ Summary blib/script/oauthomatic_predefined_servers.pl »
1  
2 ## no critic (ProhibitMultiplePackages, RequireFilenameMatchesPackage, RequireUseWArnings, RequireUseStrict, RequireExplicitPackage)
3  
4 =head1 NAME
5  
6 OAuthomatic::Error - structured exceptions thrown by OAuthomatic
7  
8 =head1 DESCRIPTION
9  
10 Errors defined here allow for inspection of various error details.
11  
12 =head1 SYNOPSIS
13  
14 try {
15 OAuthomatic::Error::Sth->throw({
16 ident => 'short description',
17 # ... other params
18 });
19 } catch {
20 my $error = $_;
21 if ($error->isa('OAuthomatic::Error')) {
22 print $error->message, "\n\n", $error->stack_trace->as_string;
23 # Or use class-dependant fields
24 }
25 };
26  
27 =cut
28  
29 {
30 package OAuthomatic::Error;
31 25 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 25 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 25 ▶ sub Payload { return 'Role::HasPayload::Meta::Attribute::Payload' }
Subroutine Payload: called CC=1 SCAR=0.0
Pod undocumented
34 25 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # Some, but not all, parts of Throwable::X
37 25 with 'Throwable'; # ->throw
38 25 with 'Role::HasPayload::Merged'; # ->payload (marked attribs + explicit payload)
39 25 with 'StackTrace::Auto'; # ->stack_trace->as_string
40 # Subtypes use Role::HasMessage::Errf
41  
42 has ident => (is => 'ro', required => 1,
43 0 ▶ isa => subtype('Str', where { length && /\A\S/ && /\S\z/ }),
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
length $_ and /\A\S/ and /\S\z/ 0 0 0
Condition0%0/3
length $_ and /\A\S/
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ and /\A\S/
length $_ /\A\S/
Truth table0%0/3
A and B
A= length $_B= /\A\S/
ABresult
0X0
100
111
44 traits => [Payload]);
45  
46 use overload fallback => 1,
47 25 ▶ '""' => sub { $_[0]->message };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
48 25 };
49  
50 =head2 OAuthomatic::Error::HTTPFailure
51  
52 Object representing various communication and OAuth-protocol related failures.
53  
54 try {
55 OAuthomatic::Error::HTTPFailure->throw({
56 ident => 'OAuth HTTP request failed',
57 request => $request, # HTTP::Request
58 response => $response, # HTTP::Response
59 });
60 } catch {
61 my $error = $_;
62 if ($error->isa('OAuthomatic::Error::HTTPFailure')) {
63 print "$error\n"; # message
64 print $error->stack_trace->as_string; # if necessary
65 if($error->is_new_client_key_required) {
66 # request new client (application) key
67 } elsif($error->is_new_token_required) {
68 # redo authorization sequence
69 }
70 # See also other fields - code, uri, struct_detail
71 }
72 };
73  
74 =head3 METHODS
75  
76 =over 4
77  
78 =item C<is_new_client_key_required()>
79  
80 Do details of this error mean, that OAuth client key in use is no longer valid and should be replaced?
81  
82 =item C<is_new_token_required()>
83  
84 Do details of this error mean, that OAuth token in use is no longer valid and application
85 should get new one?
86  
87 =back
88  
89 =head3 ATTRIBUTES
90  
91 =over 4
92  
93 =item C<request>
94  
95 L<HTTP::Request> object containing request which caused failure.
96  
97 =item C<response>
98  
99 L<HTTP::Response> object containing obtained reply (error reply).
100  
101 =item C<code>
102  
103 Shortcut. HTTP error code (400, 401, 500, ...).
104  
105 =item C<status>
106  
107 Shortcut. HTTP status line
108  
109 =item C<oauth_problem>
110  
111 If description of actual OAuth problem was detected, appropriate text code, for
112 example C<parameter_absent>, C<token_revoked>, C<consumer_key_rejected>, ...
113  
114 See L<http://wiki.oauth.net/w/page/12238543/ProblemReporting> for possible values.
115  
116 =item C<detail>
117  
118 Error detail. Formatted from information available in response content (if format
119 was not recognized, this very content by itself).
120  
121 =item C<struct_detail>
122  
123 Deserialized error detail in case output contains form-encoded data. Handles:
124  
125 =over 4
126  
127 =item form-serialized data
128  
129 Frequently used in OAuth initial protocol sequences, for example you may see here:
130  
131 {
132 oauth_problem => 'parameter_absent',
133 oauth_parameters_absent => 'oauth_consumer_key',
134 }
135  
136 =item JSON error output
137  
138 For example
139  
140 {
141 error => { id => '9e9c7bddeff3',
142 message => 'Object already deleted' },
143 }
144  
145 =back
146  
147 =item C<method>
148  
149 Shortcut. HTTP method (GET, POST, PUT, DELETE)
150  
151 =item C<uri>
152  
153 Shortcut. URI object representing the call.
154  
155 =back
156  
157 =cut
158  
159 {
160 package OAuthomatic::Error::HTTPFailure;
161 25 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 25 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 25 ▶ use OAuthomatic::Internal::Util qw/parse_http_msg_form parse_http_msg_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 25 ▶ use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
165 25 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 25 extends 'OAuthomatic::Error';
168 25 with 'Role::HasMessage::Errf' => {
169 default => "OAuthomatic HTTP failure: %{ident}s.\n"
170 . " Code: %{code}s. Status: %{status}s\n"
171 . " Call: %{method}s %{uri}s\n"
172 . " %{detail}s",
173 };
174  
175 125 ▶ sub Payload { return 'Role::HasPayload::Meta::Attribute::Payload' }
Subroutine Payload: called CC=1 SCAR=0.0
Pod undocumented
176  
177 25 has request => (is => 'ro', isa => 'HTTP::Request', required => 1);
178 25 has response => (is => 'ro', isa => 'HTTP::Response', required => 1);
179  
180 25 has code => (is => 'ro', lazy_build => 1, traits => [Payload]);
181 25 has status => (is => 'ro', lazy_build => 1, traits => [Payload]);
182 25 has method => (is => 'ro', lazy_build => 1, traits => [Payload]);
183 25 has uri => (is => 'ro', lazy_build => 1, traits => [Payload]);
184 25 has detail => (is => 'ro', lazy_build => 1, traits => [Payload]);
185 # In some cases we get form-encoded error attributes, if they
186 # are present, we keep them there
187 25 has struct_detail => (is => 'ro', lazy_build => 1);
188 # Detailed info about problem, if any, http://wiki.oauth.net/w/page/12238543/ProblemReporting
189 25 has oauth_problem => (is => 'ro', lazy_build => 1);
190  
191 sub is_new_client_key_required {
192 0 ▶ my $self = shift;
Subroutine is_new_client_key_required: not called CC=3 SCAR=24.8
Pod documented
193 0 ▶ my $problem = $self->oauth_problem || '';
Condition0%0/2
$self->oauth_problem || ''
l!l
00
MC/DC0%0/1
$self->oauth_problem || ''
$self->oauth_problem
Truth table0%0/2
A
A= $self->oauth_problem
Aresult
11
00
194 0 ▶ if($problem =~ /^(consumer_key_unknown|consumer_key_rejected)$/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($problem =~ /^(consumer_key_unknown|consumer_key_rejected)$/x) 0 0 0
195 0 return 1;
196 }
197 0 return 0;
198 }
199  
200 sub is_new_token_required {
201 0 ▶ my $self = shift;
Subroutine is_new_token_required: not called CC=3 SCAR=24.8
Pod documented
202 0 ▶ my $problem = $self->oauth_problem || '';
Condition0%0/2
$self->oauth_problem || ''
l!l
00
MC/DC0%0/1
$self->oauth_problem || ''
$self->oauth_problem
Truth table0%0/2
A
A= $self->oauth_problem
Aresult
11
00
203 0 ▶ if($problem =~ /^(token_expired|token_revoked|token_rejected|permission_unknown|permission_denied)$/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($problem =~ /^(token_expired|token_revoked|token_rejected|permission_unknown|permission_denied)$/x) 0 0 0
204 0 return 1;
205 }
206 0 return 0;
207 }
208  
209 ## FIXME: implement by delegation?
210  
211 ## no critic (RequireArgUnpacking)
212 sub _build_code {
213 0 ▶ return $_[0]->response->code;
Subroutine _build_code: not called CC=1 SCAR=6.9
214 }
215 sub _build_status {
216 0 ▶ return $_[0]->response->message;
Subroutine _build_status: not called CC=1 SCAR=6.9
217 }
218 sub _build_method {
219 0 ▶ return $_[0]->request->method;
Subroutine _build_method: not called CC=1 SCAR=6.9
220 }
221 sub _build_uri {
222 0 ▶ return $_[0]->request->uri;
Subroutine _build_uri: not called CC=1 SCAR=6.9
223 }
224 ## use critic
225  
226 sub _build_struct_detail {
227 0 ▶ my $self = shift;
Subroutine _build_struct_detail: not called CC=4 SCAR=30.0
228 0 my $reply;
229  
230 0 my $response = $self->response;
231 0 ▶ return unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
232  
233 0 my $content_type = $response->content_type;
234 # my $charset = $response->content_type_charset;
235  
236 # HTML form errors. Some real examples:
237 # (in headers)
238 # Content-Type: application/x-www-form-urlencoded;charset=UTF-8
239 # (in body)
240 # oauth_parameters_absent=oauth_consumer_key%26oauth_signature_method%26oauth_signature%26oauth_timestamp&oauth_problem=parameter_absent
241 # (or)
242 # oauth_parameters_absent=oauth_consumer_key&oauth_problem=parameter_absent
243 0 ▶ if($content_type eq 'application/x-www-form-urlencoded') {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type eq 'application/x-www-form-urlencoded') { } 0 0 0
elsif ($content_type =~ m[^application/(?:x-)?json]x) { } 0 0 0
244 try {
245 0 ▶ $reply = parse_http_msg_form($response, 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 };
247 }
248 elsif($content_type =~ m{^application/(?:x-)?json}x) {
249 try {
250 0 ▶ $reply = parse_http_msg_json($response);
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 };
252 }
253  
254 # FIXME: maybe compact JSON level up if it contains just 'error'
255  
256 # FIXME: XML errors (LinkedIn for example)
257  
258 0 return $reply;
259 }
260  
261 sub _build_oauth_problem {
262 0 ▶ my $self = shift;
Subroutine _build_oauth_problem: not called CC=3 SCAR=24.8
263 0 my $struct_detail = $self->struct_detail;
264 0 ▶ if($struct_detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct_detail) 0 0 0
265 0 ▶ if(exists $struct_detail->{oauth_problem}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $struct_detail->{"oauth_problem"}) 0 0 0
266 0 return $struct_detail->{oauth_problem};
267 }
268 }
269 0 return ''; # To make comparisons easier
270 }
271  
272 sub _build_detail {
273 0 ▶ my $self = shift;
Subroutine _build_detail: not called CC=2 SCAR=17.9
274  
275 0 my $struct_detail = $self->struct_detail;
276 0 my $detail_text;
277 0 ▶ if($struct_detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct_detail) { } 0 0 0
278 0 local $Data::Dump::INDENT = " ";
279 0 $detail_text = dump($struct_detail);
280 0 chomp($detail_text);
281 } else {
282 0 $detail_text = $self->response->decoded_content;
283 0 chomp($detail_text);
284 }
285 0 $detail_text =~ s{\r?\n}{\n }xg;
286 0 return "Details:\n " . $detail_text . "\n";
287 }
288 25 };
289  
290 =head2 OAuthomatic::Error::Generic
291  
292 Object representing non-HTTP related exception (mostly various cases of bad parameters
293 and programming errors).
294  
295 try {
296 OAuthomatic::Error::Generic->throw({
297 ident => 'Required parameter missing',
298 extra => "Neither body, nor body_params provided."
299 });
300 } catch {
301 my $error = $_;
302 if ($error->isa('OAuthomatic::Error::Generic')) {
303 print "$error\n"; # message
304 print $error->stack_trace->as_string; # if necessary
305 }
306 };
307  
308 =head3 ATTRIBUTES
309  
310 =over 4
311  
312 =item C<ident>
313  
314 Short error description
315  
316 =item C<extra>
317  
318 Additional, more elaborate, information.
319  
320 =back
321  
322 =cut
323  
324 {
325 25 package OAuthomatic::Error::Generic;
326 25 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
327 25 extends 'OAuthomatic::Error';
328 25 with 'Role::HasMessage::Errf' => {
329 default => "OAuthomatic internal error: %{ident}s.\n"
330 . "%{extra}s\n",
331 };
332  
333 25 ▶ sub Payload { return 'Role::HasPayload::Meta::Attribute::Payload' }
Subroutine Payload: called CC=1 SCAR=0.0
Pod undocumented
334  
335 25 has extra => (is => 'ro', isa => 'Str', traits => [Payload]);
336 };
337  
338 25 1;
« blib/lib/OAuthomatic/SecretStorage/Keyring.pm ↑ Summary blib/script/oauthomatic_predefined_servers.pl »