diff Lib/IMPL/Web/Handler/ErrorHandler.pm @ 206:c8fe3f84feba

+IMPL::Web::Handlers::ViewSelector +IMPL::Web::Handlers::ErrorHandler *IMPL::Web::Handlers::RestController moved types mappings to ViewSelector
author sergey
date Thu, 03 May 2012 16:48:39 +0400
parents
children 3d433a977e3b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/Web/Handler/ErrorHandler.pm	Thu May 03 16:48:39 2012 +0400
@@ -0,0 +1,70 @@
+package IMPL::Web::Handler::ErrorHandler;
+use strict;
+
+use IMPL::lang qw(:declare :constants is);
+use IMPL::Exception();
+use IMPL::declare {
+	require => {
+		WebException => 'IMPL::Web::Exception',
+		ArgumentException => '-IMPL::InvalidArgumentException',
+	},
+	base => {
+		'IMPL::Object' => undef,
+		'IMPL::Object::Autofill' => '@_',
+		'IMPL::Object::Serializable' => undef
+	}
+};
+
+BEGIN {
+	public property errors => PROP_ALL;
+	public property loader => PROP_ALL;
+	public property fallback => PROP_ALL;
+	public property contentType => PROP_ALL;
+}
+
+sub CTOR {
+	my ($this) = @_;
+	
+	die ArgumentException->new("loader") unless $this->loader;
+	die ArgumentException->new("fallback") unless $this->fallback;
+	
+	$this->errors({}) unless $this->errors;
+	
+}
+
+sub Invoke {
+	my ($this,$action,$next) = @_;
+	
+	undef $@;
+	my $result;
+	eval {
+        $result = $next ? $next->($action) : undef;
+	};
+	
+	if (my $err = $@) {
+		$action->ReinitResponse();
+		$action->response->contentType($this->contentType);
+		
+		my $vars = {
+			error => $err
+		};
+		
+		my $code = 500;
+		
+		$code =  $err->code if eval { $err->isa(WebException) };
+		
+		$action->response->status("$code");
+	
+		my $doc = $this->loader->document(
+            $this->errors->{$code} || $this->fallback,
+            $vars
+        );
+        
+        my $hout = $action->response->streamBody;
+        print $hout $doc->Render($vars);
+	}
+	
+	return $result;
+}
+
+1;
\ No newline at end of file