Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 357:ec58c47edb52
web security: code cleanup, refactoring
author | cin |
---|---|
date | Fri, 18 Oct 2013 15:10:10 +0400 |
parents | feeb3bc4a818 |
children | 69a1f1508696 |
rev | line source |
---|---|
206 | 1 package IMPL::Web::Handler::ErrorHandler; |
2 use strict; | |
3 | |
233 | 4 use IMPL::Const qw(:prop); |
206 | 5 use IMPL::Exception(); |
6 use IMPL::declare { | |
7 require => { | |
8 WebException => 'IMPL::Web::Exception', | |
9 ArgumentException => '-IMPL::InvalidArgumentException', | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
10 IOException => '-IMPL::IOException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
11 HttpResponse => 'IMPL::Web::HttpResponse' |
206 | 12 }, |
13 base => { | |
14 'IMPL::Object' => undef, | |
15 'IMPL::Object::Autofill' => '@_', | |
16 'IMPL::Object::Serializable' => undef | |
233 | 17 }, |
18 props => [ | |
19 errors => PROP_RW, | |
353 | 20 view => PROP_RW, |
21 layout => PROP_RW, | |
233 | 22 fallback => PROP_RW, |
23 contentType => PROP_RW | |
24 ] | |
206 | 25 }; |
26 | |
27 sub CTOR { | |
28 my ($this) = @_; | |
29 | |
353 | 30 die ArgumentException->new("view") unless $this->view; |
206 | 31 die ArgumentException->new("fallback") unless $this->fallback; |
32 | |
33 $this->errors({}) unless $this->errors; | |
34 | |
35 } | |
36 | |
37 sub Invoke { | |
38 my ($this,$action,$next) = @_; | |
39 | |
40 undef $@; | |
41 my $result; | |
42 eval { | |
43 $result = $next ? $next->($action) : undef; | |
44 }; | |
45 | |
46 if (my $err = $@) { | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
233
diff
changeset
|
47 |
353 | 48 warn "error handler: $err"; |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
233
diff
changeset
|
49 |
206 | 50 my $vars = { |
51 error => $err | |
52 }; | |
53 | |
230 | 54 my $status = "500 Internal Server Error"; |
206 | 55 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
56 if (eval { $err->isa(WebException) }) { |
230 | 57 $status = $err->status; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
58 } |
206 | 59 |
230 | 60 my ($code) = ($status =~ m/^(\d+)/); |
61 | |
353 | 62 my $text = $this->view->display( |
63 $err, | |
64 $this->errors->{$code} || $this->fallback, | |
65 { | |
66 layout => $this->layout | |
67 } | |
68 ); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
69 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
70 $result = HttpResponse->new( |
230 | 71 status => $status, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
72 type => $this->contentType, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
73 charset => 'utf-8', |
230 | 74 headers => eval{ $err->headers } || {}, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
75 body => $text |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
76 ); |
206 | 77 } |
78 | |
79 return $result; | |
80 } | |
81 | |
213 | 82 1; |
83 | |
84 __END__ | |
85 | |
86 =pod | |
87 | |
88 =head1 NAME | |
89 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
90 C<IMPL::Web::Handler::ErrorHandler> - обертка для обработки исключений. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
91 |
213 | 92 =head1 SYNOPSIS |
93 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
94 Используется в цеопчке обработчиков приложения. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
95 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
96 =begin code xml |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
97 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
98 <handlers type="ARRAY"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
99 <item type="IMPL::Web::Handler::ErrorHandler"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
100 <contentType>text/html</contentType> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
101 <loader refid="tt-loader"/> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
102 <errors type="HASH"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
103 <error extname="500">errors/500</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
104 <error extname="404">errors/404</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
105 <error extname="403">errors/403</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
106 </errors> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
107 <fallback>errors/500</fallback> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
108 </item> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
109 </handlers> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
110 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
111 =end code xml |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
112 |
213 | 113 =head1 DESCRIPTION |
114 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
115 Позволяет создать представление для ресурса в случае ошибки, для этого |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
116 используется соответствие представлений и кодов ошибок. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
117 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
118 В результате обработчик либо прозрачно передает результат вышестоящего |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
119 обработчика нижестоящему, либо создает C<IMPL::Web::HttpResponse> с |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
120 соответствующим статусом и содержанием. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
121 |
213 | 122 =cut |