Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 231:ff1e8fa932f2
sync
author | sergey |
---|---|
date | Tue, 09 Oct 2012 03:09:41 +0400 |
parents | 6d8092d8ce1b |
children | 3cebcf6fdb9b |
rev | line source |
---|---|
206 | 1 package IMPL::Web::Handler::ErrorHandler; |
2 use strict; | |
3 | |
4 use IMPL::lang qw(:declare :constants is); | |
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 | |
17 } | |
18 }; | |
19 | |
20 BEGIN { | |
21 public property errors => PROP_ALL; | |
22 public property loader => PROP_ALL; | |
23 public property fallback => PROP_ALL; | |
24 public property contentType => PROP_ALL; | |
25 } | |
26 | |
27 sub CTOR { | |
28 my ($this) = @_; | |
29 | |
30 die ArgumentException->new("loader") unless $this->loader; | |
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 = $@) { | |
47 | |
48 my $vars = { | |
49 error => $err | |
50 }; | |
51 | |
230 | 52 my $status = "500 Internal Server Error"; |
206 | 53 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
54 if (eval { $err->isa(WebException) }) { |
230 | 55 $status = $err->status; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
56 } |
206 | 57 |
230 | 58 my ($code) = ($status =~ m/^(\d+)/); |
59 | |
206 | 60 my $doc = $this->loader->document( |
61 $this->errors->{$code} || $this->fallback, | |
62 $vars | |
63 ); | |
64 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
65 my $text = $doc->Render($vars); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
66 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
67 $result = HttpResponse->new( |
230 | 68 status => $status, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
69 type => $this->contentType, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
70 charset => 'utf-8', |
230 | 71 headers => eval{ $err->headers } || {}, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
72 body => $text |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
73 ); |
206 | 74 } |
75 | |
76 return $result; | |
77 } | |
78 | |
213 | 79 1; |
80 | |
81 __END__ | |
82 | |
83 =pod | |
84 | |
85 =head1 NAME | |
86 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
87 C<IMPL::Web::Handler::ErrorHandler> - обертка для обработки исключений. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
88 |
213 | 89 =head1 SYNOPSIS |
90 | |
229
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
93 =begin code xml |
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 <handlers type="ARRAY"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
96 <item type="IMPL::Web::Handler::ErrorHandler"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
97 <contentType>text/html</contentType> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
98 <loader refid="tt-loader"/> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
99 <errors type="HASH"> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
100 <error extname="500">errors/500</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
101 <error extname="404">errors/404</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
102 <error extname="403">errors/403</error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
103 </errors> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
104 <fallback>errors/500</fallback> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
105 </item> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
106 </handlers> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
107 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
108 =end code xml |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
109 |
213 | 110 =head1 DESCRIPTION |
111 | |
229
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 В результате обработчик либо прозрачно передает результат вышестоящего |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
116 обработчика нижестоящему, либо создает C<IMPL::Web::HttpResponse> с |
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 |
213 | 119 =cut |