view Lib/IMPL/Web/Handler/ErrorHandler.pm @ 217:bfce101e0a5a

Слияние
author cin
date Mon, 20 Aug 2012 10:27:20 +0400
parents d6e2ea24af08
children 47f77e6409f7
line wrap: on
line source

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->charset('utf-8');
		$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;

__END__

=pod

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut