annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 207:f534a60d5b01

minor changes
author sergey
date Fri, 04 May 2012 02:09:13 +0400
parents c8fe3f84feba
children 3d433a977e3b
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
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
4 use IMPL::lang qw(:declare :constants is);
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',
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
10 },
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
11 base => {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
12 'IMPL::Object' => undef,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
13 'IMPL::Object::Autofill' => '@_',
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
14 'IMPL::Object::Serializable' => undef
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
15 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
16 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
17
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
18 BEGIN {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
19 public property errors => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
20 public property loader => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
21 public property fallback => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
22 public property contentType => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
23 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
24
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
25 sub CTOR {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
26 my ($this) = @_;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
27
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
28 die ArgumentException->new("loader") unless $this->loader;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
29 die ArgumentException->new("fallback") unless $this->fallback;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
30
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
31 $this->errors({}) unless $this->errors;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
32
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
33 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
34
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
35 sub Invoke {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
36 my ($this,$action,$next) = @_;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
37
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
38 undef $@;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
39 my $result;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
40 eval {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
41 $result = $next ? $next->($action) : undef;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
42 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
43
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
44 if (my $err = $@) {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
45 $action->ReinitResponse();
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
46 $action->response->contentType($this->contentType);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
47
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
48 my $vars = {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
49 error => $err
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
50 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
51
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
52 my $code = 500;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
53
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
54 $code = $err->code if eval { $err->isa(WebException) };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
55
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
56 $action->response->status("$code");
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
57
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
58 my $doc = $this->loader->document(
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
59 $this->errors->{$code} || $this->fallback,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
60 $vars
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
61 );
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
62
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
63 my $hout = $action->response->streamBody;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
64 print $hout $doc->Render($vars);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
65 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
66
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
67 return $result;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
68 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
69
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
70 1;