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();
|
|
46 $action->response->contentType($this->contentType);
|
|
47
|
|
48 my $vars = {
|
|
49 error => $err
|
|
50 };
|
|
51
|
|
52 my $code = 500;
|
|
53
|
|
54 $code = $err->code if eval { $err->isa(WebException) };
|
|
55
|
|
56 $action->response->status("$code");
|
|
57
|
|
58 my $doc = $this->loader->document(
|
|
59 $this->errors->{$code} || $this->fallback,
|
|
60 $vars
|
|
61 );
|
|
62
|
|
63 my $hout = $action->response->streamBody;
|
|
64 print $hout $doc->Render($vars);
|
|
65 }
|
|
66
|
|
67 return $result;
|
|
68 }
|
|
69
|
|
70 1; |