annotate lib/IMPL/Exception.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Exception;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use overload
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 '""' => \&ToString,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 'fallback' => 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use Carp qw(longmess shortmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use Scalar::Util qw(refaddr);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 require Error;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 use parent qw(IMPL::Object::Abstract Error Class::Accessor);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 __PACKAGE__->mk_accessors( qw(Message Args CallStack Source) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 sub indent {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 my ($str,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 $level ||= 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 $str = '' unless defined $str;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 join ("\n", map( " "x$level.$_ , split(/\n/,$str) ) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 sub new {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my $class = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $class = ref $class || $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my $this = $class->Error::new() or die "Failed to create an exception";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 $this->callCTOR(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $this->{-text} = $this->Message;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 local $Carp::CarpLevel = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 $this->CallStack(longmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 $this->Source(shortmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 return $this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my ($this,$message,@args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 $this->Message($message || '');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 die new IMPL::Exception("Fatal erorr: cyclic structure in the exceptions were detected, do not use \$\@ while throwing the exception!") if grep ref $_ ? refaddr($this) == refaddr($_) : 0 , @args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 $this->Args([map defined $_ ? $_ : 'undef', @args]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 sub save {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my ($this,$ctx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $ctx->AddVar(Message => $this->Message) if $this->Message;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 $ctx->AddVar(Args => $this->Args) if @{$this->Args};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 $ctx->AddVar(Source => $this->Source);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 $ctx->AddVar(CallStack => $this->CallStack);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 sub restore {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my ($class,$data,$instance) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 my %args = @$data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 if ($instance) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $instance->callCTOR($args{Message},@{$args{Args}});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 $instance = $class->new($args{Message},@{$args{Args}});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $instance->Source($args{Source});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $instance->CallStack($args{CallStack});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 return $instance;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 sub ToString {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 $this->toString();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 sub toString {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 my ($this,$notrace) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 ($this->Message || ref $this) . join("\n",'',map { my $s = $_; local $_; indent("$s",1) } @{$this->Args} ) . ( $notrace ? '' : "\n" . $this->CallStack);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 package IMPL::InvalidOperationException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 package IMPL::InvalidArgumentException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 'IMPL::Exception' => sub { "An invalid argument", @_ }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 package IMPL::DuplicateException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 package IMPL::KeyNotFoundException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 'IMPL::Exception' => sub { "A specified element isn't found", $_[0] }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 package IMPL::NotImplementedException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 package IMPL::SecurityException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 package IMPL::AccessDeniedException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 our @ISA = qw(IMPL::SecurityException);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 our %CTOR = ( 'IMPL::SecurityException' => sub { 'Access denied' ,@_ } );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 package Exception;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 package IMPL::DeprecatedException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 'IMPL::Exception' => sub { @_ ? @_ : "The method is deprecated" }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 package IMPL::WrongDataException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 'IMPL::Exception' => sub { "The input data is wrong", @_ }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 package IMPL::IOException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 our @ISA = qw(IMPL::Exception);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 __PACKAGE__->PassThroughArgs;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 1;