comparison Lib/IMPL/AppException.pm @ 253:0a228a35645c

added application exception class
author sergey
date Mon, 19 Nov 2012 17:54:01 +0400
parents
children 93963ec449c5
comparison
equal deleted inserted replaced
252:34a3f8668b58 253:0a228a35645c
1 package IMPL::AppException;
2 use strict;
3 use mro 'c3';
4 use overload
5 '""' => 'ToString',
6 'bool' => sub { return 1; },
7 'fallback' => 1;
8
9 use Carp qw(longmess shortmess);
10 use Scalar::Util qw(refaddr);
11
12 use IMPL::Const qw(:prop);
13 use IMPL::Resources::Strings {
14 messageFormat => "Application exception"
15 };
16
17 use IMPL::declare {
18 base => [
19 'IMPL::Object' => '@_'
20 ],
21 props => [
22 source => PROP_RO,
23 callStack => PROP_RO,
24 _cachedMessage => PROP_RW
25 ]
26 };
27
28 sub new {
29 my $self = shift;
30 local $Carp::CarpLevel = 0;
31
32 my $instance = $self->next::method(@_);
33
34 $instance->callStack(longmess);
35 $instance->source(shortmess);
36
37 return $instance;
38 }
39
40
41 sub message {
42 my ($this) = @_;
43
44 if (my $msg = $this->_cachedMessage) {
45 return $msg;
46 } else {
47 my $formatter = $this->can('messageFormat');
48 return $this->_cachedMessage($formatter->($this));
49 }
50 }
51
52 sub ToString {
53 my ($this) = @_;
54
55 return join("\n", $this->message, $this->callStack);
56 }
57
58 1;