# HG changeset patch # User sergey # Date 1353333241 -14400 # Node ID 0a228a35645c09f01982fa893a3090df9f9e364a # Parent 34a3f8668b58a89c3dcb06b37a8c27a202de90a8 added application exception class diff -r 34a3f8668b58 -r 0a228a35645c Lib/IMPL/AppException.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/IMPL/AppException.pm Mon Nov 19 17:54:01 2012 +0400 @@ -0,0 +1,58 @@ +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' => '@_' + ], + props => [ + source => PROP_RO, + callStack => PROP_RO, + _cachedMessage => PROP_RW + ] +}; + +sub new { + my $self = shift; + local $Carp::CarpLevel = 0; + + my $instance = $self->next::method(@_); + + $instance->callStack(longmess); + $instance->source(shortmess); + + 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); +} + +1; \ No newline at end of file