line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::CustomErrorMessage; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Catalyst::Plugin::CustomErrorMessage - Catalyst plugin to have more "cute" error message. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Catalyst qw( CustomErrorMessage ); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# optional (values in this example are the defaults) |
12
|
|
|
|
|
|
|
__PACKAGE__->config->{'custom-error-message'}->{'uri-for-not-found'} = '/'; |
13
|
|
|
|
|
|
|
__PACKAGE__->config->{'custom-error-message'}->{'error-template'} = 'error.tt2'; |
14
|
|
|
|
|
|
|
__PACKAGE__->config->{'custom-error-message'}->{'content-type'} = 'text/html; charset=utf-8'; |
15
|
|
|
|
|
|
|
__PACKAGE__->config->{'custom-error-message'}->{'view-name'} = 'TT'; |
16
|
|
|
|
|
|
|
__PACKAGE__->config->{'custom-error-message'}->{'response-status'} = 500; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
You can use this module if you want to get rid of: |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
(en) Please come back later |
23
|
|
|
|
|
|
|
(fr) SVP veuillez revenir plus tard |
24
|
|
|
|
|
|
|
(de) Bitte versuchen sie es spaeter nocheinmal |
25
|
|
|
|
|
|
|
(at) Konnten's bitt'schoen spaeter nochmal reinschauen |
26
|
|
|
|
|
|
|
(no) Vennligst prov igjen senere |
27
|
|
|
|
|
|
|
(dk) Venligst prov igen senere |
28
|
|
|
|
|
|
|
(pl) Prosze sprobowac pozniej |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
What it does is that it inherites finalize_error to $c object. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
See finalize_error() function. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
69576
|
use base qw{ Class::Data::Inheritable }; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5143
|
|
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
3635
|
use HTML::Entities; |
|
1
|
|
|
|
|
26332
|
|
|
1
|
|
|
|
|
116
|
|
39
|
1
|
|
|
1
|
|
1167
|
use URI::Escape qw{ uri_escape_utf8 }; |
|
1
|
|
|
|
|
4609
|
|
|
1
|
|
|
|
|
66
|
|
40
|
1
|
|
|
1
|
|
2067
|
use MRO::Compat; |
|
1
|
|
|
|
|
7603
|
|
|
1
|
|
|
|
|
36
|
|
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
43
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
620
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our $VERSION = "0.06"; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 FUNCTIONS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 finalize_error |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
In debug mode this function is skipped and user sees the original Catalyst error page in debug mode. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
In "production" (non debug) mode it will return page with template set in |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$c->config->{'custom-error-message'}->{'error-template'} |
57
|
|
|
|
|
|
|
|| |
58
|
|
|
|
|
|
|
'error.tt2' |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$c->stash->{'finalize_error'} will be set to contain the error message. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
For non existing resources (like misspelled url-s) if will do http redirect to |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$c->uri_for( |
65
|
|
|
|
|
|
|
$c->config->{'custom-error-message'}->{'uri-for-not-found'} |
66
|
|
|
|
|
|
|
|| |
67
|
|
|
|
|
|
|
'/' |
68
|
|
|
|
|
|
|
) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$c->flash->{'finalize_error'} will be set to contain the error message. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
To set different view name configure: |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$c->config->{'custom-error-message'}->{'view-name'} = 'Mason'; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Content-type and response status can be configured via: |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$c->config->{'custom-error-message'}->{'content-type'} = 'text/plain; charset=utf-8'; |
79
|
|
|
|
|
|
|
$c->config->{'custom-error-message'}->{'response-status'} = 500; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub finalize_error { |
84
|
6
|
|
|
6
|
1
|
13396
|
my $c = shift; |
85
|
6
|
|
66
|
|
|
21
|
my $config = $c->config->{'custom-error-message'} || $c->config->{'custome-error-messsage'} || $c->config->{'custom-error-messsage'}; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# in debug mode return the original "page" |
88
|
6
|
100
|
|
|
|
106
|
if ( $c->debug ) { |
89
|
1
|
|
|
|
|
15
|
$c->maybe::next::method; |
90
|
1
|
|
|
|
|
16
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# create error string out of error array |
94
|
5
|
|
|
|
|
31
|
my $error = join ' ', map { encode_entities($_) } @{ $c->error }; |
|
10
|
|
|
|
|
115
|
|
|
5
|
|
|
|
|
16
|
|
95
|
5
|
|
50
|
|
|
68
|
$error ||= 'No output'; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# for wrong url that has no action associated do redirect |
98
|
5
|
100
|
|
|
|
21
|
if (not defined $c->action) { |
99
|
2
|
|
|
|
|
18
|
$c->flash->{'finalize_error'} = $error; |
100
|
2
|
|
|
|
|
18
|
$c->_save_flash(); # hack but must be called otherwise the flash data will be lost |
101
|
2
|
|
100
|
|
|
13
|
$c->response->redirect($c->uri_for( |
102
|
|
|
|
|
|
|
$config->{'uri-for-not-found'} |
103
|
|
|
|
|
|
|
|| |
104
|
|
|
|
|
|
|
'/' |
105
|
|
|
|
|
|
|
)); |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
51
|
return; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# render the template |
111
|
3
|
|
|
|
|
24
|
my $action_name = $c->action->reverse; |
112
|
3
|
|
|
|
|
33
|
$c->stash->{'finalize_error'} = $action_name.': '.$error; |
113
|
3
|
|
100
|
|
|
20
|
$c->response->content_type( |
114
|
|
|
|
|
|
|
$config->{'content-type'} |
115
|
|
|
|
|
|
|
|| |
116
|
|
|
|
|
|
|
'text/html; charset=utf-8' |
117
|
|
|
|
|
|
|
); |
118
|
3
|
|
100
|
|
|
54
|
my $view_name = $config->{'view-name'} || 'TT'; |
119
|
3
|
|
|
|
|
8
|
eval { |
120
|
3
|
|
100
|
|
|
9
|
$c->response->body($c->view($view_name)->render($c, |
121
|
|
|
|
|
|
|
$config->{'error-template'} |
122
|
|
|
|
|
|
|
|| |
123
|
|
|
|
|
|
|
'error.tt2' |
124
|
|
|
|
|
|
|
)); |
125
|
|
|
|
|
|
|
}; |
126
|
3
|
100
|
|
|
|
228
|
if ($@) { |
127
|
1
|
|
|
|
|
7
|
$c->log->error($@); |
128
|
1
|
|
|
|
|
42
|
$c->maybe::next::method; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
3
|
|
|
|
|
17
|
my $response_status = $config->{'response-status'}; |
132
|
3
|
100
|
|
|
|
8
|
$response_status = 500 if not defined $response_status; |
133
|
3
|
|
|
|
|
10
|
$c->response->status($response_status); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
1; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 AUTHOR |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Jozef Kutej - Ejkutej@cpan.orgE |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Copyright (C) 2006 by Jozef Kutej |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
147
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.4 or, |
148
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |