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',
|
|
10 },
|
|
11 base => {
|
|
12 'IMPL::Object' => undef,
|
|
13 'IMPL::Object::Autofill' => '@_',
|
|
14 'IMPL::Object::Serializable' => undef
|
|
15 }
|
|
16 };
|
|
17
|
|
18 BEGIN {
|
|
19 public property errors => PROP_ALL;
|
|
20 public property loader => PROP_ALL;
|
|
21 public property fallback => PROP_ALL;
|
|
22 public property contentType => PROP_ALL;
|
|
23 }
|
|
24
|
|
25 sub CTOR {
|
|
26 my ($this) = @_;
|
|
27
|
|
28 die ArgumentException->new("loader") unless $this->loader;
|
|
29 die ArgumentException->new("fallback") unless $this->fallback;
|
|
30
|
|
31 $this->errors({}) unless $this->errors;
|
|
32
|
|
33 }
|
|
34
|
|
35 sub Invoke {
|
|
36 my ($this,$action,$next) = @_;
|
|
37
|
|
38 undef $@;
|
|
39 my $result;
|
|
40 eval {
|
|
41 $result = $next ? $next->($action) : undef;
|
|
42 };
|
|
43
|
|
44 if (my $err = $@) {
|
|
45 $action->ReinitResponse();
|
208
|
46 $action->response->charset('utf-8');
|
206
|
47 $action->response->contentType($this->contentType);
|
|
48
|
|
49 my $vars = {
|
|
50 error => $err
|
|
51 };
|
|
52
|
|
53 my $code = 500;
|
|
54
|
|
55 $code = $err->code if eval { $err->isa(WebException) };
|
|
56
|
|
57 $action->response->status("$code");
|
|
58
|
|
59 my $doc = $this->loader->document(
|
|
60 $this->errors->{$code} || $this->fallback,
|
|
61 $vars
|
|
62 );
|
|
63
|
|
64 my $hout = $action->response->streamBody;
|
|
65 print $hout $doc->Render($vars);
|
|
66 }
|
|
67
|
|
68 return $result;
|
|
69 }
|
|
70
|
|
71 1; |