line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yukki::Error; |
2
|
|
|
|
|
|
|
$Yukki::Error::VERSION = '0.991_002'; # TRIAL |
3
|
|
|
|
|
|
|
|
4
|
5
|
|
|
5
|
|
217134
|
$Yukki::Error::VERSION = '0.991002';use v5.24; |
|
5
|
|
|
|
|
17
|
|
5
|
5
|
|
|
5
|
|
27
|
use utf8; |
|
5
|
|
|
|
|
29
|
|
|
5
|
|
|
|
|
50
|
|
6
|
5
|
|
|
5
|
|
606
|
use Moo; |
|
5
|
|
|
|
|
14991
|
|
|
5
|
|
|
|
|
54
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends qw( HTTP::Throwable::Factory ); |
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
5710
|
use Yukki::Web::View; |
|
5
|
|
|
|
|
19
|
|
|
5
|
|
|
|
|
224
|
|
11
|
|
|
|
|
|
|
|
12
|
5
|
|
|
5
|
|
47
|
use namespace::clean; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
54
|
|
13
|
|
|
|
|
|
|
|
14
|
5
|
|
|
|
|
69
|
use Sub::Exporter -setup => { |
15
|
|
|
|
|
|
|
exports => [ qw< http_throw http_exception > ], |
16
|
5
|
|
|
5
|
|
2649
|
}; |
|
5
|
|
|
|
|
19958
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# ABSTRACT: Yukki's exception class |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
1
|
12911
|
sub base_class { 'Yukki::Error' } |
22
|
1
|
|
|
1
|
1
|
12
|
sub extra_roles { 'Yukki::Error::Body' } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub http_exception { |
26
|
1
|
|
|
1
|
1
|
3
|
my ($name, $args) = @_; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
50
|
|
|
2
|
my %args = %{ $args // {} }; |
|
1
|
|
|
|
|
6
|
|
29
|
1
|
|
50
|
|
|
4
|
my $status = delete $args{status} // 'InternalServerError'; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
18
|
Yukki::Error->new_exception($status => { |
32
|
|
|
|
|
|
|
%args, |
33
|
|
|
|
|
|
|
message => "$name", |
34
|
|
|
|
|
|
|
}); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub http_throw { |
39
|
1
|
|
|
1
|
1
|
3
|
my ($name, $args) = @_; |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
4
|
http_exception($name, $args)->throw; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub BUILDARGS { |
45
|
1
|
|
|
1
|
0
|
13870
|
my ($class, $args) = @_; |
46
|
1
|
|
|
|
|
15
|
$args; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
{ |
51
|
|
|
|
|
|
|
package Yukki::Error::Body; |
52
|
|
|
|
|
|
|
$Yukki::Error::Body::VERSION = '0.991_002'; # TRIAL |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
5
|
|
3816
|
$Yukki::Error::Body::VERSION = '0.991002';use Moo::Role; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
49
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub body { |
58
|
0
|
|
|
0
|
0
|
0
|
my ($self, $env) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my $app = $env->{'yukki.app'}; |
61
|
0
|
|
|
|
|
0
|
my $view = Yukki::Web::View->new(app => $app); |
62
|
0
|
|
|
|
|
0
|
my $ctx = Yukki::Web::Context->new(env => $env); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my $template = $view->prepare_template( |
65
|
|
|
|
|
|
|
template => 'error.html', |
66
|
|
|
|
|
|
|
directives => [ |
67
|
|
|
|
|
|
|
'#error-page' => 'error_message', |
68
|
|
|
|
|
|
|
], |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
$ctx->response->page_title($self->reason); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
return $view->render_page( |
74
|
|
|
|
|
|
|
template => $template, |
75
|
|
|
|
|
|
|
context => $ctx, |
76
|
|
|
|
|
|
|
vars => { |
77
|
|
|
|
|
|
|
'error_message' => $self->message, |
78
|
|
|
|
|
|
|
}, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub body_headers { |
84
|
0
|
|
|
0
|
0
|
0
|
my ($self, $body) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return [ |
87
|
0
|
|
|
|
|
0
|
'Content-type' => 'text/html', |
88
|
|
|
|
|
|
|
'Content-length' => length $body, |
89
|
|
|
|
|
|
|
]; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub as_string { |
94
|
7
|
|
|
7
|
0
|
611
|
my $self = shift; |
95
|
7
|
|
|
|
|
23
|
return $self->message; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
around as_psgi => sub { |
99
|
|
|
|
|
|
|
shift; # original method is ignored |
100
|
|
|
|
|
|
|
my ($self, $env) = @_; |
101
|
|
|
|
|
|
|
my $body = $self->body( $env ); |
102
|
|
|
|
|
|
|
my $headers = $self->build_headers( $body ); |
103
|
|
|
|
|
|
|
[ $self->status_code, $headers, [ defined $body ? $body : () ] ]; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=pod |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=encoding UTF-8 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 NAME |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Yukki::Error - Yukki's exception class |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 VERSION |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
version 0.991_002 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SYNOPSIS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Yukki::Error->throw("Something really bad.", { ... }); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 DESCRIPTION |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
If you are familiar with L<HTTP::Throwable::Factory>, this is similar to that (and is based on that). |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
However, there are two differences. First, the error message is given primacy rather than exception type, so you can just use this to throw an exception: |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
use Yukki::Error qw( http_throw ); |
135
|
|
|
|
|
|
|
http_throw('something went wrong'); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Since you almost always want your exception to be an internal server error of some kind, this makes more sense to me than having to write: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use HTTP::Throwable::Factory qw( http_throw ); |
140
|
|
|
|
|
|
|
http_throw(InternalServerError => { |
141
|
|
|
|
|
|
|
message => 'something went wrong', |
142
|
|
|
|
|
|
|
}); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
To specify the type of exception, us C<status>: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
use Yukki::Error qw( http_throw ); |
147
|
|
|
|
|
|
|
http_throw('something was not found', { |
148
|
|
|
|
|
|
|
status => 'NotFound', |
149
|
|
|
|
|
|
|
}); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
The second difference is that all exceptions thrown by this factory inherit from L<Yukki::Error>, so this works: |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
use Scalar::Util qw( blessed ); |
154
|
|
|
|
|
|
|
use Try::Tiny; |
155
|
|
|
|
|
|
|
try { ... } |
156
|
|
|
|
|
|
|
catch { |
157
|
|
|
|
|
|
|
if (blassed $_ && $_->isa("Yukki::Error") { |
158
|
|
|
|
|
|
|
# we now this is an application error from Yukki |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
}; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This makes it easy to know whether Yukki generated the exception or something else did. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 EXPORTS |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 http_exception |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $error = http_exception('message', { |
169
|
|
|
|
|
|
|
status => 'InternalServerError', |
170
|
|
|
|
|
|
|
show_stask_trace => 0, |
171
|
|
|
|
|
|
|
}); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Creates a new exception object. Calls the constructor for L<Yukki:Error> and applied the L<HTTP::Throwable> status role needed (prior to construction actually). |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 http_throw |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
http_throw('message', { |
178
|
|
|
|
|
|
|
status => 'InternalServerError', |
179
|
|
|
|
|
|
|
show_stask_trace => 0, |
180
|
|
|
|
|
|
|
}); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Constructs the exception (via L</http_exception>) and throws it. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=for Pod::Coverage BUILDARGS |
185
|
|
|
|
|
|
|
base_class |
186
|
|
|
|
|
|
|
extra_roles |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 METHODS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 body |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Renders the HTML body for the error. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 body_headers |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Setup the HTTP headers. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 as_string |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Returns the message. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 AUTHOR |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Andrew Sterling Hanenkamp <hanenkamp@cpan.org> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Qubling Software LLC. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
211
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |