annotate Lib/IMPL/Web/Handler/ErrorHandler.pm @ 216:e9fd7ff3f54c

sync
author sergey
date Thu, 09 Aug 2012 17:24:07 +0400
parents d6e2ea24af08
children 47f77e6409f7
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();
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
46 $action->response->charset('utf-8');
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
47 $action->response->contentType($this->contentType);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
48
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
49 my $vars = {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
50 error => $err
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
51 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
52
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
53 my $code = 500;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
54
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
55 $code = $err->code if eval { $err->isa(WebException) };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
56
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
57 $action->response->status("$code");
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
58
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
59 my $doc = $this->loader->document(
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
60 $this->errors->{$code} || $this->fallback,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
61 $vars
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
62 );
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
63
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
64 my $hout = $action->response->streamBody;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
65 print $hout $doc->Render($vars);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
66 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
67
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
68 return $result;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
69 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
70
213
sergey
parents: 208
diff changeset
71 1;
sergey
parents: 208
diff changeset
72
sergey
parents: 208
diff changeset
73 __END__
sergey
parents: 208
diff changeset
74
sergey
parents: 208
diff changeset
75 =pod
sergey
parents: 208
diff changeset
76
sergey
parents: 208
diff changeset
77 =head1 NAME
sergey
parents: 208
diff changeset
78
sergey
parents: 208
diff changeset
79 =head1 SYNOPSIS
sergey
parents: 208
diff changeset
80
sergey
parents: 208
diff changeset
81 =head1 DESCRIPTION
sergey
parents: 208
diff changeset
82
sergey
parents: 208
diff changeset
83 =cut