changeset 253:0a228a35645c

added application exception class
author sergey
date Mon, 19 Nov 2012 17:54:01 +0400
parents 34a3f8668b58
children fb52014f6931
files Lib/IMPL/AppException.pm
diffstat 1 files changed, 58 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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