view Lib/IMPL/AppException.pm @ 269:dacfe7c0311a

SQL schema updated (unstable)
author sergey
date Thu, 24 Jan 2013 20:00:27 +0400
parents 89179bb8c388
children 4ddb27ff4a0b
line wrap: on
line source

package IMPL::AppException;
use strict;
use mro 'c3';
use overload
    '""' => 'ToString',
    'bool'     =>   sub { return 1; },
    'fallback' => 1;

use Carp qw(longmess shortmess);
use Scalar::Util qw(refaddr);

use IMPL::Const qw(:prop);
use IMPL::Resources::Strings {
	messageFormat => "Application exception"
};

use IMPL::declare {
    base => [
        'IMPL::Object' => undef
    ],
    props => [
        source => PROP_RO,
        callStack => PROP_RO,
        _cachedMessage => PROP_RW 
    ]
};

sub new {
	my $self = shift;
	
	my $instance = $self->next::method(@_);
	
	$instance->source(shortmess);
	$instance->callStack(longmess);
    
    return $instance;
}


sub message {
	my ($this) = @_;
	
	if (my $msg = $this->_cachedMessage) {
		return $msg;
	} else {
	   my $formatter = $this->can('messageFormat');
	   return $this->_cachedMessage($formatter->($this));
	}
}

sub ToString {
	my ($this) = @_;
	
	return join("\n", $this->message, $this->callStack);
}

sub throw {
	my $self = shift;
	
	die $self->new(@_);
}

1;