blib/lib/OAuthomatic/Error.pm

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