Mercurial > pub > Impl
comparison Lib/IMPL/Web/Handler/ErrorHandler.pm @ 229:47f77e6409f7
heavily reworked the resource model of the web application:
*some ResourcesContraact functionality moved to Resource
+Added CustomResource
*Corrected action handlers
author | sergey |
---|---|
date | Sat, 29 Sep 2012 02:34:47 +0400 |
parents | d6e2ea24af08 |
children | 6d8092d8ce1b |
comparison
equal
deleted
inserted
replaced
228:431db7034a88 | 229:47f77e6409f7 |
---|---|
5 use IMPL::Exception(); | 5 use IMPL::Exception(); |
6 use IMPL::declare { | 6 use IMPL::declare { |
7 require => { | 7 require => { |
8 WebException => 'IMPL::Web::Exception', | 8 WebException => 'IMPL::Web::Exception', |
9 ArgumentException => '-IMPL::InvalidArgumentException', | 9 ArgumentException => '-IMPL::InvalidArgumentException', |
10 IOException => '-IMPL::IOException', | |
11 HttpResponse => 'IMPL::Web::HttpResponse' | |
10 }, | 12 }, |
11 base => { | 13 base => { |
12 'IMPL::Object' => undef, | 14 'IMPL::Object' => undef, |
13 'IMPL::Object::Autofill' => '@_', | 15 'IMPL::Object::Autofill' => '@_', |
14 'IMPL::Object::Serializable' => undef | 16 'IMPL::Object::Serializable' => undef |
40 eval { | 42 eval { |
41 $result = $next ? $next->($action) : undef; | 43 $result = $next ? $next->($action) : undef; |
42 }; | 44 }; |
43 | 45 |
44 if (my $err = $@) { | 46 if (my $err = $@) { |
45 $action->ReinitResponse(); | |
46 $action->response->charset('utf-8'); | |
47 $action->response->contentType($this->contentType); | |
48 | 47 |
49 my $vars = { | 48 my $vars = { |
50 error => $err | 49 error => $err |
51 }; | 50 }; |
52 | 51 |
53 my $code = 500; | 52 my $code = 500; |
54 | 53 |
55 $code = $err->code if eval { $err->isa(WebException) }; | 54 if (eval { $err->isa(WebException) }) { |
55 ($code) = ($err->status =~ m/^(\d+)/); | |
56 } | |
56 | 57 |
57 $action->response->status("$code"); | |
58 | |
59 my $doc = $this->loader->document( | 58 my $doc = $this->loader->document( |
60 $this->errors->{$code} || $this->fallback, | 59 $this->errors->{$code} || $this->fallback, |
61 $vars | 60 $vars |
62 ); | 61 ); |
63 | 62 |
64 my $hout = $action->response->streamBody; | 63 my $text = $doc->Render($vars); |
65 print $hout $doc->Render($vars); | 64 |
65 $result = HttpResponse->new( | |
66 status => $err->status, | |
67 type => $this->contentType, | |
68 charset => 'utf-8', | |
69 headers => $err->headers, | |
70 body => $text | |
71 ); | |
66 } | 72 } |
67 | 73 |
68 return $result; | 74 return $result; |
69 } | 75 } |
70 | 76 |
74 | 80 |
75 =pod | 81 =pod |
76 | 82 |
77 =head1 NAME | 83 =head1 NAME |
78 | 84 |
85 C<IMPL::Web::Handler::ErrorHandler> - обертка для обработки исключений. | |
86 | |
79 =head1 SYNOPSIS | 87 =head1 SYNOPSIS |
88 | |
89 Используется в цеопчке обработчиков приложения. | |
90 | |
91 =begin code xml | |
92 | |
93 <handlers type="ARRAY"> | |
94 <item type="IMPL::Web::Handler::ErrorHandler"> | |
95 <contentType>text/html</contentType> | |
96 <loader refid="tt-loader"/> | |
97 <errors type="HASH"> | |
98 <error extname="500">errors/500</error> | |
99 <error extname="404">errors/404</error> | |
100 <error extname="403">errors/403</error> | |
101 </errors> | |
102 <fallback>errors/500</fallback> | |
103 </item> | |
104 </handlers> | |
105 | |
106 =end code xml | |
80 | 107 |
81 =head1 DESCRIPTION | 108 =head1 DESCRIPTION |
82 | 109 |
110 Позволяет создать представление для ресурса в случае ошибки, для этого | |
111 используется соответствие представлений и кодов ошибок. | |
112 | |
113 В результате обработчик либо прозрачно передает результат вышестоящего | |
114 обработчика нижестоящему, либо создает C<IMPL::Web::HttpResponse> с | |
115 соответствующим статусом и содержанием. | |
116 | |
83 =cut | 117 =cut |