annotate lib/IMPL/Web/Handler/ErrorHandler.pm @ 407:c6e90e02dd17 ref20150831

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