Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 253:0a228a35645c
added application exception class
author | sergey |
---|---|
date | Mon, 19 Nov 2012 17:54:01 +0400 |
parents | 3cebcf6fdb9b |
children | 32aceba4ee6d |
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, | |
20 loader => PROP_RW, | |
21 fallback => PROP_RW, | |
22 contentType => PROP_RW | |
23 ] | |
206 | 24 }; |
25 | |
26 sub CTOR { | |
27 my ($this) = @_; | |
28 | |
29 die ArgumentException->new("loader") unless $this->loader; | |
30 die ArgumentException->new("fallback") unless $this->fallback; | |
31 | |
32 $this->errors({}) unless $this->errors; | |
33 | |
34 } | |
35 | |
36 sub Invoke { | |
37 my ($this,$action,$next) = @_; | |
38 | |
39 undef $@; | |
40 my $result; | |
41 eval { | |
42 $result = $next ? $next->($action) : undef; | |
43 }; | |
44 | |
45 if (my $err = $@) { | |
46 | |
47 my $vars = { | |
48 error => $err | |
49 }; | |
50 | |
230 | 51 my $status = "500 Internal Server Error"; |
206 | 52 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
53 if (eval { $err->isa(WebException) }) { |
230 | 54 $status = $err->status; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
55 } |
206 | 56 |
230 | 57 my ($code) = ($status =~ m/^(\d+)/); |
58 | |
206 | 59 my $doc = $this->loader->document( |
60 $this->errors->{$code} || $this->fallback, | |
61 $vars | |
62 ); | |
63 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
64 my $text = $doc->Render($vars); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
65 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
66 $result = HttpResponse->new( |
230 | 67 status => $status, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
68 type => $this->contentType, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
69 charset => 'utf-8', |
230 | 70 headers => eval{ $err->headers } || {}, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
71 body => $text |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
72 ); |
206 | 73 } |
74 | |
75 return $result; | |
76 } | |
77 | |
213 | 78 1; |
79 | |
80 __END__ | |
81 | |
82 =pod | |
83 | |
84 =head1 NAME | |
85 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
86 C<IMPL::Web::Handler::ErrorHandler> - обертка для обработки исключений. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
87 |
213 | 88 =head1 SYNOPSIS |
89 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
90 Используется в цеопчке обработчиков приложения. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
91 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
92 =begin code xml |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
93 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
94 <handlers type="ARRAY"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
95 <item type="IMPL::Web::Handler::ErrorHandler"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
96 <contentType>text/html</contentType> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
97 <loader refid="tt-loader"/> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
98 <errors type="HASH"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
99 <error extname="500">errors/500</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
100 <error extname="404">errors/404</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
101 <error extname="403">errors/403</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
102 </errors> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
103 <fallback>errors/500</fallback> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
104 </item> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
105 </handlers> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
106 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
107 =end code xml |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
108 |
213 | 109 =head1 DESCRIPTION |
110 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
111 Позволяет создать представление для ресурса в случае ошибки, для этого |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
112 используется соответствие представлений и кодов ошибок. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
113 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
114 В результате обработчик либо прозрачно передает результат вышестоящего |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
115 обработчика нижестоящему, либо создает C<IMPL::Web::HttpResponse> с |
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 |
213 | 118 =cut |