Jifty/Plugin/ErrorTemplates/View.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 125 CC 5 SCAR 26.9
CC
5
Cov
0%
maybe_page
30.0
wrapper
2.0
BEGIN
2.0
CRAP 14.7
1 package Jifty::Plugin::ErrorTemplates::View;
2  
3 use strict;
4 use warnings;
5  
6 use Jifty::View::Declare -base;
7  
8 use Scalar::Defer;
9  
10 =head1 NAME
11  
12 Jifty::Plugin::ErrorTemplates::View - Template pages to show errors
13  
14 =head1 DESCRIPTION
15  
16 Default error templates
17  
18 =cut
19  
20  
21 template '/error/_elements/error_text' => sub {
22 my ($error) = get(qw(error));
23 h1 { 'Sorry, something went awry' };
24 p {
25 _(
26 "For one reason or another, you got to a web page that caused a bit of an error. And then you got to our 'basic' error handler. Which means we haven't written a pretty, easy to understand error message for you just yet. The message we do have is:"
27 );
28 };
29  
30 blockquote {
31 b { $error || "Internal server error" };
32 };
33  
34 p {
35 _(
36 "There's a pretty good chance that error message doesn't mean anything to you, but we'd rather you have a little bit of information about what went wrong than nothing. We've logged this error, so we know we need to write something friendly explaining just what happened and how to fix it."
37 );
38 };
39  
40 p {
41 hyperlink(
42 url => "/",
43 label => _('Head on back home')
44 );
45 _("for now, and try to forget that we let you down.");
46 };
47 };
48  
49 =head2 wrapper
50  
51 This exists as a fallback wrapper, in case the error in question is
52 caused by the Jifty application's wrapper, for instance.
53  
54 =cut
55  
56 {
57 no warnings qw'redefine';
58  
59 sub wrapper {
60 my $code = shift;
61 html {
62 head {
63 title { _('Internal error') } link {
64 attr {
65 rel => 'stylesheet',
66 type => 'text/css',
67 href => "/__jifty/error/error.css",
68 media => 'all'
69 }
70 };
71 }
72 body {
73 h1 {'Internal Error'};
74 Jifty->web->render_messages;
75 $code->();
76 }
77 }
78 }
79 }
80  
81  
82 template '__jifty/error/error.css' => sub {
83 Jifty->web->response->content_type("text/css");
84 h1 {
85 outs('color: red');
86 };
87 };
88  
89 =head2 maybe_page
90  
91 Like L<Jifty::View::Declare::Helpers/page>, but only outputs a page
92 wrapper if the request is not a subrequest.
93  
94 =cut
95  
96 sub maybe_page (&;$) {
97 unshift @_, undef unless @_ == 2;
98 my ($meta, $code) = @_;
99 my $ret = sub {
100 if (Jifty->web->request->is_subrequest) {
101 local *is::title = sub {};
102 $code->();
103 } else {
104 page {$meta ? $meta->() : () } content {$code->()};
105 }
106 };
107 $ret->() unless defined wantarray;
108 return $ret;
109 }
110  
111 template '/errors/404' => sub {
112 my $file = get('path') || Jifty->web->request->path;
113 Jifty->log->info( "404: user tried to get to " . $file );
114 Jifty->web->response->status( 404 )
115 unless Jifty->web->request->is_subrequest;
116 maybe_page { title => _("Something's not quite right") } content {
117 with( id => "overview" ), div {
118 p {
119 join( " ",
120 _("You got to a page that we don't think exists."),
121 _("Anyway, the software has logged this error."),
122 _("Sorry about this.") );
123 }
124  
125 p {
126 hyperlink(
127 url => "/",
128 label => _('Go back home...')
129 );
130 }
131  
132 }
133 };
134 };
135  
136  
137  
138 template '/errors/500' => maybe_page { title => _('Something went awry') } content {
139 my $cont = Jifty->web->request->continuation;
140  
141 # If we're not in devel, bail
142 if ( not Jifty->config->framework("DevelMode") or not $cont ) {
143 show("/error/_elements/error_text");
144 return;
145 }
146  
147 my $e = $cont->response->error;
148 if ( ref($e) ) {
149 my $msg = $e->message;
150 $msg =~ s/, <\S+> (line|chunk) \d+\././;
151  
152 pre {$msg};
153  
154 form {
155 form_return( label => _("Try again") );
156 };
157  
158 h2 {'Call stack'};
159 ul {
160 for my $frame (@{$e->template_stack}) {
161 li { $frame }
162 }
163 };
164 } else {
165 pre {$e};
166 }
167 };
168  
169 1;