407
|
1 package IMPL::Web::Handler::ErrorHandler;
|
|
2 use strict;
|
|
3
|
|
4 use IMPL::Const qw(:prop);
|
|
5 use IMPL::Exception();
|
|
6 use IMPL::declare {
|
|
7 require => {
|
|
8 WebException => 'IMPL::Web::Exception',
|
|
9 ArgumentException => '-IMPL::InvalidArgumentException',
|
|
10 IOException => '-IMPL::IOException',
|
|
11 HttpResponse => 'IMPL::Web::HttpResponse',
|
|
12 Security => 'IMPL::Security'
|
|
13 },
|
|
14 base => {
|
|
15 'IMPL::Object' => undef,
|
|
16 'IMPL::Object::Autofill' => '@_',
|
|
17 'IMPL::Object::Serializable' => undef
|
|
18 },
|
|
19 props => [
|
|
20 errors => PROP_RW,
|
|
21 view => PROP_RW,
|
|
22 fallback => PROP_RW,
|
|
23 contentType => PROP_RW
|
|
24 ]
|
|
25 };
|
|
26
|
|
27 sub CTOR {
|
|
28 my ($this) = @_;
|
|
29
|
|
30 die ArgumentException->new("view") unless $this->view;
|
|
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 result => $result,
|
|
51 request => sub { $action },
|
|
52 app => $action->application,
|
|
53 location => $action->context->{resourceLocation},
|
|
54 resource => $action->context->{resource},
|
|
55 document => {},
|
|
56 session => sub { Security->context },
|
|
57 user => sub { Security->principal },
|
|
58 security => sub { $action->security }
|
|
59 };
|
|
60
|
|
61 my $status = "500 Internal Server Error";
|
|
62
|
|
63 if (eval { $err->isa(WebException) }) {
|
|
64 $status = $err->status;
|
|
65 }
|
|
66
|
|
67 my ($code) = ($status =~ m/^(\d+)/);
|
|
68
|
|
69 my $text = $this->view->display(
|
|
70 $err,
|
|
71 $this->errors->{$code} || $this->fallback,
|
|
72 $vars
|
|
73 );
|
|
74
|
|
75 $result = HttpResponse->new(
|
|
76 status => $status,
|
|
77 type => $this->contentType,
|
|
78 charset => 'utf-8',
|
|
79 headers => eval{ $err->headers } || {},
|
|
80 body => $text
|
|
81 );
|
|
82 }
|
|
83
|
|
84 return $result;
|
|
85 }
|
|
86
|
|
87 1;
|
|
88
|
|
89 __END__
|
|
90
|
|
91 =pod
|
|
92
|
|
93 =head1 NAME
|
|
94
|
|
95 C<IMPL::Web::Handler::ErrorHandler> - обертка для обработки исключений.
|
|
96
|
|
97 =head1 SYNOPSIS
|
|
98
|
|
99 Используется в цеопчке обработчиков приложения.
|
|
100
|
|
101 =begin code xml
|
|
102
|
|
103 <handlers type="ARRAY">
|
|
104 <item type="IMPL::Web::Handler::ErrorHandler">
|
|
105 <contentType>text/html</contentType>
|
|
106 <loader refid="tt-loader"/>
|
|
107 <errors type="HASH">
|
|
108 <error extname="500">errors/500</error>
|
|
109 <error extname="404">errors/404</error>
|
|
110 <error extname="403">errors/403</error>
|
|
111 </errors>
|
|
112 <fallback>errors/500</fallback>
|
|
113 </item>
|
|
114 </handlers>
|
|
115
|
|
116 =end code xml
|
|
117
|
|
118 =head1 DESCRIPTION
|
|
119
|
|
120 Позволяет создать представление для ресурса в случае ошибки, для этого
|
|
121 используется соответствие представлений и кодов ошибок.
|
|
122
|
|
123 В результате обработчик либо прозрачно передает результат вышестоящего
|
|
124 обработчика нижестоящему, либо создает C<IMPL::Web::HttpResponse> с
|
|
125 соответствующим статусом и содержанием.
|
|
126
|
|
127 =cut |