SelectPdf/DemoExceptions.pm

filter: Statementstmt 92.8% 78 / 84 Branchbran 62.5% 30 / 48 Conditioncond 47.6% 10 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 88.0% 22 / 25 Podpod n/a totaltotal 74.4% 143 / 192 CC 32 SCAR 19.8
CC
32
Cov
77%
fromResponse
16.2
new
9.9
new
6.8
CRAP 7.2
« SelectPdf/HtmlToPdfClient.pm ↑ Summary SelectPdf/AsyncJobClient.pm »
1 package SelectPdf::DemoException;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use overload
5 1 ▶ '""' => sub { $_[0]->message() },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.0
6 0 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
7 1 fallback => 1;
8
9 1 our $VERSION = '1.6.0';
10
11 # Default upgrade URL displayed in demo-mode error messages.
12 1 ▶use constant UPGRADE_URL => "https://selectpdf.com/pricing/";
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 =head1 NAME
15
16 SelectPdf::DemoException - Base class for the typed errors raised by the keyless demo endpoint.
17
18 =head1 SYNOPSIS
19
20 The demo endpoint (used when C<SelectPdf::HtmlToPdfClient> is constructed without an API key) reports
21 rate limits, rejected URLs and unsupported features as structured JSON. The client turns those into
22 exception objects that are thrown with C<die>. They stringify to a readable message, so code that only
23 prints C<$@> keeps working, and they can be inspected programmatically:
24
25 use SelectPdf;
26
27 eval {
28 my $client = SelectPdf::HtmlToPdfClient->new(); # keyless demo mode
29 $client->convertUrlToFile("https://selectpdf.com", "Test.pdf");
30 };
31
32 if (my $err = $@) {
33 if (ref $err && $err->isa('SelectPdf::DemoRateLimitException')) {
34 # reason is one of: per_ip, daily_cap, concurrency
35 print "Demo rate limit (" . $err->reason() . "). Retry after " . $err->retryAfter() . "s. Upgrade: " . $err->upgradeUrl() . "\n";
36 }
37 elsif (ref $err && $err->isa('SelectPdf::DemoSafetyException')) {
38 print "Demo safety guard rejected '" . $err->field() . "' (reason=" . $err->reason() . ").\n";
39 }
40 elsif (ref $err && $err->isa('SelectPdf::DemoUnsupportedException')) {
41 print "Feature '" . $err->field() . "' not available in demo mode. Upgrade: " . $err->upgradeUrl() . "\n";
42 }
43 else {
44 print "An error occurred: $err\n";
45 }
46 }
47
48 The classes are:
49
50 =over
51
52 =item * C<SelectPdf::DemoRateLimitException> - HTTP 429 or 503, the demo rate limit was reached.
53
54 =item * C<SelectPdf::DemoSafetyException> - HTTP 400, a URL field references a non-public host.
55
56 =item * C<SelectPdf::DemoUnsupportedException> - HTTP 400 (or raised locally before the request is sent), the feature is not available in demo mode.
57
58 =back
59
60 All of them derive from C<SelectPdf::DemoException>.
61
62 =head1 METHODS
63
64 =head2 message
65
66 The error message. The object also stringifies to this message.
67
68 =head2 statusCode
69
70 HTTP status code returned by the server. Zero for a C<SelectPdf::DemoUnsupportedException> raised by the local client guard before the request was sent.
71
72 =head2 responseBody
73
74 The raw JSON body the server returned, for diagnostics / logging. Undef when the exception was raised by the local client guard.
75
76 =cut
77 6 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
78 6 my($type, %args) = @_;
79 6 my $self = { %args };
80 6 bless $self, $type;
81 6 return $self;
82 }
83
84 1 ▶sub message {
Subroutine message: called CC=1 SCAR=0.0
85 1 my($self) = @_;
86 1 return $self->{message};
87 }
88
89 1 ▶sub statusCode {
Subroutine statusCode: called CC=1 SCAR=0.0
90 1 my($self) = @_;
91 1 return $self->{statusCode};
92 }
93
94 0 ▶sub responseBody {
Subroutine responseBody: not called CC=1 SCAR=6.9
95 0 my($self) = @_;
96 0 return $self->{responseBody};
97 }
98
99 # Build the typed demo exception that corresponds to a JSON error body returned by the API,
100 # exactly as the .NET client does (TryBuildDemoException):
101 # {"error":"rate_limited","reason":"per_ip","upgrade":"..."} -> SelectPdf::DemoRateLimitException
102 # {"error":"unsafe_url","field":"url","reason":"private_ip"} -> SelectPdf::DemoSafetyException
103 # {"error":"unsupported_in_demo","field":"user_password","upgrade":"..."} -> SelectPdf::DemoUnsupportedException
104 # {"error":"body_too_large","max_bytes":1048576,"upgrade":"..."} -> plain error message (string)
105 #
106 # Returns the exception (object or string) or undef if the body is not a recognized demo error.
107 5 ▶sub fromResponse {
Subroutine fromResponse: called CC=14 SCAR=27.9
108 5 my($class, $statusCode, $body, $retryAfter) = @_;
109
110 5 ▶ return undef if (not defined($body) or $body eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $body or $body eq "" 0 5 5
Condition33%1/3
not defined $body or $body eq ""
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $body or $body eq ""
not defined $body $body eq ""
Truth table33%1/3
not(A) or B
A= not defined $bodyB= $body eq ""
ABresult
1X1
011
000
111
112 5 my $json = eval {
113 5 require JSON;
114 5 JSON->new->allow_nonref->decode($body);
115 };
116 5 ▶ return undef if (not defined($json) or ref($json) ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $json or ref $json ne "HASH" 1 4 5
Condition66%2/3
not defined $json or ref $json ne "HASH"
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $json or ref $json ne "HASH"
not defined $json ref $json ne "HASH"
Truth table66%2/3
not(A) or B
A= not defined $jsonB= ref $json ne "HASH"
ABresult
1X1
011
000
117
118 4 my $error = _stringField($json, "error");
119 4 ▶ return undef if (not defined($error) or $error eq "");
Branch: 1 branch
BranchTrueFalseTotal
if not defined $error or $error eq "" 0 4 4
Condition33%1/3
not defined $error or $error eq ""
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $error or $error eq ""
not defined $error $error eq ""
Truth table33%1/3
not(A) or B
A= not defined $errorB= $error eq ""
ABresult
1X1
011
000
120
121 4 my $reason = _stringField($json, "reason");
122 4 my $field = _stringField($json, "field");
123 4 my $upgrade = _stringField($json, "upgrade");
124
125 4 ▶ $retryAfter = 0 if (not defined($retryAfter) or $retryAfter !~ m/^\s*\d+\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retryAfter and $retryAfter =~ /^\s*\d+\s*$/ 3 1 4
Condition66%2/3
defined $retryAfter and $retryAfter =~ /^\s*\d+\s*$/
!ll&&!rl&&r
301
MC/DC50%1/2
defined $retryAfter and $retryAfter =~ /^\s*\d+\s*$/
defined $retryAfter $retryAfter =~ /^\s*\d+\s*$/
Truth table66%2/3
A and B
A= defined $retryAfterB= $retryAfter =~ /^\s*\d+\s*$/
ABresult
0X0
100
111
126 4 $retryAfter = int($retryAfter);
127
128 4 ▶ if ($error eq "rate_limited") {
Branch: 4 branches
BranchTrueFalseTotal
if ($error eq 'rate_limited') { } 1 3 4
elsif ($error eq 'unsafe_url') { } 1 2 3
elsif ($error eq 'unsupported_in_demo') { } 1 1 2
elsif ($error eq 'body_too_large') { } 1 0 1
129 1 return SelectPdf::DemoRateLimitException->new($statusCode, $reason, $retryAfter, $upgrade, $body);
130 }
131 elsif ($error eq "unsafe_url") {
132 1 return SelectPdf::DemoSafetyException->new($statusCode, $field, $reason, $body);
133 }
134 elsif ($error eq "unsupported_in_demo") {
135 1 return SelectPdf::DemoUnsupportedException->new($statusCode, $field, $upgrade, $body);
136 }
137 elsif ($error eq "body_too_large") {
138 1 ▶ return sprintf("(%s) Demo request body exceeds the demo cap. Upgrade at %s.",
Branch: 1 branch
BranchTrueFalseTotal
defined $upgrade ? : 1 0 1
139 $statusCode, (defined($upgrade) ? $upgrade : UPGRADE_URL));
140 }
141
142 0 return undef;
143 }
144
145 16 ▶sub _stringField {
Subroutine _stringField: called CC=3 SCAR=11.0
146 16 my($json, $name) = @_;
147 16 my $value = $json->{$name};
148 16 ▶ return undef if (not defined($value) or ref($value));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 5 11 16
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
5011
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
149 11 return "$value";
150 }
151
152 package SelectPdf::DemoRateLimitException;
153
154 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 our @ISA = qw(SelectPdf::DemoException);
156
157 =head1 NAME
158
159 SelectPdf::DemoRateLimitException - Thrown when the demo endpoint refuses a request because of a rate limit (HTTP 429 or 503).
160
161 =head1 METHODS
162
163 =head2 statusCode
164
165 HTTP status code returned by the server (429 or 503).
166
167 =head2 reason
168
169 Machine-readable rate-limit reason: C<per_ip> (the source IP exceeded its hourly conversion budget),
170 C<concurrency> (too many demo conversions are running right now) or C<daily_cap> (the global demo budget for today has been reached).
171
172 =head2 retryAfter
173
174 Seconds the client should wait before retrying (parsed from the C<Retry-After> response header). Zero if absent.
175
176 =head2 upgradeUrl
177
178 URL the user can visit to upgrade out of demo mode.
179
180 =head2 responseBody
181
182 The raw JSON body the server returned.
183
184 =cut
185 1 ▶sub new {
Subroutine new: called CC=7 SCAR=22.9
186 1 my($type, $statusCode, $reason, $retryAfter, $upgradeUrl, $body) = @_;
187
188 1 ▶ my $upgrade = (defined($upgradeUrl) and $upgradeUrl ne "") ? $upgradeUrl : SelectPdf::DemoException::UPGRADE_URL;
Branch: 1 branch
BranchTrueFalseTotal
defined $upgradeUrl && $upgradeUrl ne '' ? : 1 0 1
Condition33%1/3
defined $upgradeUrl and $upgradeUrl ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $upgradeUrl and $upgradeUrl ne ""
defined $upgradeUrl $upgradeUrl ne ""
Truth table33%1/3
A and B
A= defined $upgradeUrlB= $upgradeUrl ne ""
ABresult
0X0
100
111
189 1 ▶ $retryAfter = 0 if (not defined($retryAfter));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retryAfter 0 1 1
190 1 ▶ my $ra = $retryAfter > 0 ? sprintf(" Retry after %ss.", $retryAfter) : "";
Branch: 1 branch
BranchTrueFalseTotal
$retryAfter > 0 ? : 1 0 1
191
192 1 ▶ return $type->SUPER::_new(
Branch: 2 branches
BranchTrueFalseTotal
defined $reason ? : 1 0 1
defined $reason ? : 1 0 1
193 message => sprintf("(%s) Demo rate limit reached (reason=%s).%s Upgrade at %s.",
194 $statusCode, (defined($reason) ? $reason : "?"), $ra, $upgrade),
195 statusCode => $statusCode,
196 reason => (defined($reason) ? $reason : ""),
197 retryAfter => $retryAfter,
198 upgradeUrl => $upgrade,
199 responseBody => $body,
200 );
201 }
202
203 1 ▶sub reason {
Subroutine reason: called CC=1 SCAR=0.0
204 1 my($self) = @_;
205 1 return $self->{reason};
206 }
207
208 1 ▶sub retryAfter {
Subroutine retryAfter: called CC=1 SCAR=0.0
209 1 my($self) = @_;
210 1 return $self->{retryAfter};
211 }
212
213 0 ▶sub upgradeUrl {
Subroutine upgradeUrl: not called CC=1 SCAR=6.9
214 0 my($self) = @_;
215 0 return $self->{upgradeUrl};
216 }
217
218 package SelectPdf::DemoSafetyException;
219
220 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1 our @ISA = qw(SelectPdf::DemoException);
222
223 =head1 NAME
224
225 SelectPdf::DemoSafetyException - Thrown when the demo safety guard rejects a request because a URL field references a non-public host (HTTP 400).
226
227 =head1 METHODS
228
229 =head2 statusCode
230
231 HTTP status code returned by the server (400).
232
233 =head2 field
234
235 Which input field was rejected: C<url>, C<html>, C<base_url>, C<header_url>, C<footer_url>.
236
237 =head2 reason
238
239 Why the field was rejected: C<blocked_host>, C<private_ip>, C<loopback>, C<link_local>, C<cgnat>, C<metadata>, C<multicast>,
240 C<bad_scheme>, C<bad_url>, C<inline_internal_ref:E<lt>sub-reasonE<gt>>.
241
242 =head2 responseBody
243
244 The raw JSON body the server returned.
245
246 =cut
247 1 ▶sub new {
Subroutine new: called CC=5 SCAR=18.9
248 1 my($type, $statusCode, $field, $reason, $body) = @_;
249
250 1 ▶ return $type->SUPER::_new(
Branch: 4 branches
BranchTrueFalseTotal
defined $field ? : 1 0 1
defined $reason ? : 1 0 1
defined $field ? : 1 0 1
defined $reason ? : 1 0 1
251 message => sprintf("(%s) Demo safety guard rejected %s (reason=%s). Demo conversions cannot fetch internal/private hosts.",
252 $statusCode, (defined($field) ? $field : "?"), (defined($reason) ? $reason : "?")),
253 statusCode => $statusCode,
254 field => (defined($field) ? $field : ""),
255 reason => (defined($reason) ? $reason : ""),
256 responseBody => $body,
257 );
258 }
259
260 1 ▶sub field {
Subroutine field: called CC=1 SCAR=0.0
261 1 my($self) = @_;
262 1 return $self->{field};
263 }
264
265 1 ▶sub reason {
Subroutine reason: called CC=1 SCAR=0.0
266 1 my($self) = @_;
267 1 return $self->{reason};
268 }
269
270 package SelectPdf::DemoUnsupportedException;
271
272 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
273 1 our @ISA = qw(SelectPdf::DemoException);
274
275 =head1 NAME
276
277 SelectPdf::DemoUnsupportedException - Thrown when the caller tried to use a feature that demo mode does not support,
278 most commonly PDF passwords (HTTP 400). For paid keys, this exception is never thrown.
279
280 =head1 METHODS
281
282 =head2 statusCode
283
284 HTTP status code returned by the server (400). Zero if the exception was raised by the local client guard before the request was sent.
285
286 =head2 field
287
288 Which feature is unsupported: C<user_password>, C<owner_password>, C<async>.
289
290 =head2 upgradeUrl
291
292 URL the user can visit to upgrade out of demo mode.
293
294 =head2 responseBody
295
296 The raw JSON body the server returned. Undef when raised by the local client guard.
297
298 =cut
299 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.2
300 1 my($type, $statusCode, $field, $upgradeUrl, $body) = @_;
301
302 1 ▶ my $upgrade = (defined($upgradeUrl) and $upgradeUrl ne "") ? $upgradeUrl : SelectPdf::DemoException::UPGRADE_URL;
Branch: 1 branch
BranchTrueFalseTotal
defined $upgradeUrl && $upgradeUrl ne '' ? : 1 0 1
Condition33%1/3
defined $upgradeUrl and $upgradeUrl ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $upgradeUrl and $upgradeUrl ne ""
defined $upgradeUrl $upgradeUrl ne ""
Truth table33%1/3
A and B
A= defined $upgradeUrlB= $upgradeUrl ne ""
ABresult
0X0
100
111
303
304 1 ▶ return $type->SUPER::_new(
Branch: 2 branches
BranchTrueFalseTotal
defined $field ? : 1 0 1
defined $field ? : 1 0 1
305 message => sprintf("(%s) Feature '%s' is not available in demo mode. Upgrade at %s.",
306 $statusCode, (defined($field) ? $field : "?"), $upgrade),
307 statusCode => $statusCode,
308 field => (defined($field) ? $field : ""),
309 upgradeUrl => $upgrade,
310 responseBody => $body,
311 );
312 }
313
314 # Constructor used by the local client-side guard before the request is sent.
315 3 ▶sub newLocal {
Subroutine newLocal: called CC=3 SCAR=12.0
316 3 my($type, $field) = @_;
317
318 3 ▶ return $type->SUPER::_new(
Branch: 2 branches
BranchTrueFalseTotal
defined $field ? : 3 0 3
defined $field ? : 3 0 3
319 message => sprintf("Feature '%s' is not available in demo mode. Construct SelectPdf::HtmlToPdfClient with a paid API key, or upgrade at %s.",
320 (defined($field) ? $field : "?"), SelectPdf::DemoException::UPGRADE_URL),
321 statusCode => 0,
322 field => (defined($field) ? $field : ""),
323 upgradeUrl => SelectPdf::DemoException::UPGRADE_URL,
324 responseBody => undef,
325 );
326 }
327
328 4 ▶sub field {
Subroutine field: called CC=1 SCAR=0.0
329 4 my($self) = @_;
330 4 return $self->{field};
331 }
332
333 1 ▶sub upgradeUrl {
Subroutine upgradeUrl: called CC=1 SCAR=0.0
334 1 my($self) = @_;
335 1 return $self->{upgradeUrl};
336 }
337
338 1 1;
« SelectPdf/HtmlToPdfClient.pm ↑ Summary SelectPdf/AsyncJobClient.pm »