Neaf/Exception.pm

filter: Statementstmt 100% 57 / 57 Branchbran 93.7% 15 / 16 Conditioncond 85.2% 29 / 34 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 14 / 14 Podpod 100% 8 / 8 totaltotal 88.3% 129 / 146 CC 22 SCAR 19.3
CC
22
Cov
94%
new
14.1
file_and_line
3.0
as_string
3.0
CRAP 6.9
« Neaf/View/TT.pm ↑ Summary Neaf/Request/Apache2.pm »
1 package MVC::Neaf::Exception;
2  
3 95 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 95 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 95 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::Exception - Exception class for Not Even A Framework.
10  
11 =head1 DESCRIPTION
12  
13 Currently internal signalling or L<MVC::Neaf> is based on the exception
14 mechanism. To avoid collisions with user's exceptions or Perl errors,
15 these internal exceptions are blessed into this class.
16  
17 Please see the neaf_err() function in L<MVC::Neaf>.
18  
19 By convention, C<die nnn> and C<die MVC::Neaf::Exception-E<gt>new( nnn )>
20 will be treated exactly the same by Neaf.
21  
22 B<CAUTION.> This file is mostly used internally by Neaf
23 and may change with little to no warning.
24 Please file a bug/feature request demanding a more stable interface
25 if you plan to rely on it.
26  
27 B<CAVEAT EMPTOR>.
28  
29 =cut
30  
31 95 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 95 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 95 ▶use overload '""' => "as_string";
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 95 ▶use MVC::Neaf::Util qw(bare_html_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 =head1 METHODS
38  
39 =head2 new( $@ || 500, %options )
40  
41 =head2 new( %options )
42  
43 Returns a new exception object.
44  
45 %options may include any keys as well as some Neaf-like control keys:
46  
47 =over
48  
49 =item * -status - alias for first argument.
50 If starts with 3 digits, will result in a "http error page" exception,
51 otherwise is reset to 500 and reason is updated.
52  
53 =item * -reason - details about what happened
54  
55 =item * -headers - array or hash of headers, just like that of a normal reply.
56  
57 =item * -location - indicates a redirection
58  
59 =item * -sudden - this was not an expected error (die 404 or redirect)
60 This will automatically turn on if -status cannot be parsed.
61  
62 =item * -file - where error happened
63  
64 =item * -line - where error happened
65  
66 =item * -nocaller - don't try to determine error origin via caller
67  
68 =back
69  
70 =cut
71  
72 56 ▶sub new {
Subroutine new: called CC=14 SCAR=26.5
Pod documented
73 56 my $class = shift;
74 56 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 6 50 56
75 6 my $err = shift;
76 6 push @_, -status => $err;
77 };
78 56 my %opt = @_;
79  
80 # TODO 0.30 bad rex will catch garbage if under 'C:\Program files'
81 56 ▶ ($opt{-status} || '')
Branch: 1 branch
BranchTrueFalseTotal
($opt{"-status"} || "") =~ qr/^(?:(\d\d\d)\s*)?(.*?)(?:\s+at (\S+) line (\d+)\.?)?$/s or die qq[NEAF: Bug: Regex failed unexpectedly for q{$opt{"-status"}}] 56 0 56
Condition100%2/2
$opt{'-status'} || ''
l!l
551
MC/DC100%1/1
$opt{'-status'} || ''
$opt{'-status'}
Truth table100%2/2
A
A= $opt{'-status'}
Aresult
11
00
82 =~ qr{^(?:(\d\d\d)\s*)?(.*?)(?:\s+at (\S+) line (\d+)\.?)?$}s
83 or die "NEAF: Bug: Regex failed unexpectedly for q{$opt{-status}}";
84  
85 56 ▶ $opt{-status} = $1 || 500;
Condition100%2/2
$1 || 500
l!l
4511
MC/DC100%1/1
$1 || 500
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
86 56 ▶ $opt{-reason} ||= $2 || $1 || 'unknown error';
Condition87%7/8
$opt{"-reason"} ||= $2 || $1 || "unknown error"
l!l&&r!l&&!r
5510
$2 || $1 || 'unknown error'
l!l
501
$2 or $1
l!l&&r!l&&!r
23271
MC/DC0%0/3
$opt{"-reason"} ||= $2 || $1 || "unknown error"
$opt{"-reason"} $2 $1
Truth table0%0/4
A ||= B or C
A= $opt{"-reason"}B= $2C= $1
ABCresult
1XX1
01X1
0011
0000
87 56 ▶ $opt{-sudden} ||= !$1;
Condition100%3/3
$opt{"-sudden"} ||= !$1
l!l&&r!l&&!r
11144
MC/DC100%2/2
$opt{"-sudden"} ||= !$1
$opt{"-sudden"} !$1
Truth table100%3/3
A ||= not(B)
A= $opt{"-sudden"}B= !$1
ABresult
1X1
011
000
88 56 ▶ my @caller = $opt{-nocaller} ? () : (caller(0));
Branch: 1 branch
BranchTrueFalseTotal
$opt{'-nocaller'} ? : 39 17 56
89 56 ▶ $opt{-file} ||= $3 || $caller[1];
Condition83%5/6
$opt{"-file"} ||= $3 || $caller[1]
l!l&&r!l&&!r
02729
$3 || $caller[1]
l!l&&r!l&&!r
111629
MC/DC0%0/3
$opt{"-file"} ||= $3 || $caller[1]
$opt{"-file"} $3 $caller[1]
Truth table0%0/4
A ||= B || C
A= $opt{"-file"}B= $3C= $caller[1]
ABCresult
1XX1
01X1
0011
0000
90 56 ▶ $opt{-line} ||= $4 || $caller[2];
Condition83%5/6
$opt{"-line"} ||= $4 || $caller[2]
l!l&&r!l&&!r
02729
$4 || $caller[2]
l!l&&r!l&&!r
111629
MC/DC0%0/3
$opt{"-line"} ||= $4 || $caller[2]
$opt{"-line"} $4 $caller[2]
Truth table0%0/4
A ||= B || C
A= $opt{"-line"}B= $4C= $caller[2]
ABCresult
1XX1
01X1
0011
0000
91  
92 56 return bless \%opt, $class;
93 };
94  
95 =head2 status()
96  
97 Return error code.
98  
99 =cut
100  
101 66 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
102 66 my $self = shift;
103 66 return $self->{-status};
104 };
105  
106 =head2 is_sudden()
107  
108 Tells whether error was unexpected.
109  
110 B<EXPERIMENTAL>. Name and meaning subject to change.
111  
112 =cut
113  
114 92 ▶sub is_sudden {
Subroutine is_sudden: called CC=2 SCAR=6.9
Pod documented
115 92 my $self = shift;
116 92 ▶ return $self->{-sudden} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'-sudden'} ? : 21 71 92
117 };
118  
119 =head2 as_string()
120  
121 Stringify.
122  
123 Result will start with C<MVC::Neaf:> if error was generated via
124 C<die 404> or a redirect.
125  
126 Otherwise it would look similar to the original -status.
127  
128 =cut
129  
130 8 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
131 8 my $self = shift;
132  
133 return ($self->{-sudden} ? '' : "MVC::Neaf: ")
134 8 ▶ .($self->{-location} ? "See $self->{-location}: " : '')
Branch: 2 branches
BranchTrueFalseTotal
$self->{'-sudden'} ? : 4 4 8
$self->{'-location'} ? : 2 6 8
135 . $self->reason;
136 };
137  
138 =head2 make_reply( $request )
139  
140 Returns a refault error HTML page.
141  
142 The default page is guaranteen to contain
143 the status as its one and only C<< <span> >> element,
144 the unique request-id as one and only C<< <b> >> element,
145 and the location (if any) as its one and only C<< <i> >> element.
146  
147 This page used to be a JSON but it turned out hard to debug
148 when dealing with javascript.
149  
150 =cut
151  
152 43 ▶sub make_reply {
Subroutine make_reply: called CC=3 SCAR=11.0
Pod documented
153 43 my ($self, $req) = @_;
154  
155 43 my $code = $self->{-status};
156 43 my $redirect = '';
157 43 my $request_id = $req->id;
158 43 ▶ my @headers = @{ $self->{-headers} || [] };
Condition100%2/2
$self->{'-headers'} || []
l!l
736
MC/DC100%1/1
$self->{'-headers'} || []
$self->{'-headers'}
Truth table100%2/2
A
A= $self->{'-headers'}
Aresult
11
00
159 43 ▶ if (my $where = $self->{-location}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $self->{"-location"}) 3 40 43
160 3 unshift @headers, Location => $where;
161 3 $where = bare_html_escape( $where );
162 3 $redirect = qq{<p>See <a href="$where"><i>$where</i></a></p>};
163 };
164  
165 # An in-place template to avoid rendering
166 # don't worry, be stupid!
167 43 my $content = qq{<html>
168 <head>
169 <title>Error $code</title>
170 </head>
171 <body>
172 <h1>Error <span>$code</span></h1>
173 <p>Request-id:<b>$request_id</b></p>
174 $redirect
175 <hr></hr>
176 <small>Powered by <a href="https://metacpan.org/pod/MVC::Neaf">Not even a framework<a/>.</small>
177 </body>
178 </html>
179 };
180  
181 return {
182 -status => $self->{-status},
183 43 -content => $content,
184 -type => 'text/html; charset=utf8',
185 -headers => \@headers,
186 };
187 };
188  
189 =head2 reason()
190  
191 Returns error message that was expected to cause the error.
192  
193 =cut
194  
195 20 ▶sub reason {
Subroutine reason: called CC=2 SCAR=7.2
Pod documented
196 20 my $self = shift;
197  
198 20 ▶ return ($self->{-reason} || "Unknown error") . $self->file_and_line;
Condition50%1/2
$self->{'-reason'} || 'Unknown error'
l!l
200
MC/DC0%0/1
$self->{'-reason'} || 'Unknown error'
$self->{'-reason'}
Truth table50%1/2
A
A= $self->{'-reason'}
Aresult
11
00
199 };
200  
201 =head2 file_and_line
202  
203 Return " at /foo/bar line 42" suffix, if both file and line are available.
204 Empty string otherwise.
205  
206 =cut
207  
208 22 ▶sub file_and_line {
Subroutine file_and_line: called CC=3 SCAR=11.1
Pod documented
209 22 my $self = shift;
210 return ($self->{-file} && $self->{-line})
211 22 ▶ ? " at $self->{-file} line $self->{-line}"
Branch: 1 branch
BranchTrueFalseTotal
$self->{'-file'} && $self->{'-line'} ? : 21 1 22
Condition66%2/3
$self->{"-file"} and $self->{"-line"}
!ll&&!rl&&r
1021
MC/DC50%1/2
$self->{"-file"} and $self->{"-line"}
$self->{"-file"} $self->{"-line"}
Truth table66%2/3
A and B
A= $self->{"-file"}B= $self->{"-line"}
ABresult
0X0
100
111
212 : ''
213 };
214  
215 =head2 TO_JSON()
216  
217 Converts exception to JSON, so that it doesn't frighten View::JS.
218  
219 =cut
220  
221 1 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
222 1 my $self = shift;
223 1 return { %$self };
224 };
225  
226 =head1 LICENSE AND COPYRIGHT
227  
228 This module is part of L<MVC::Neaf> suite.
229  
230 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
231  
232 This program is free software; you can redistribute it and/or modify it
233 under the terms of either: the GNU General Public License as published
234 by the Free Software Foundation; or the Artistic License.
235  
236 See L<http://dev.perl.org/licenses/> for more information.
237  
238 =cut
239  
240 95 1;
« Neaf/View/TT.pm ↑ Summary Neaf/Request/Apache2.pm »