annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 404:9ef75f2029be default

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