annotate lib/IMPL/AppException.pm @ 410:9335cf010b23 ref20150831

refactoring
author cin
date Mon, 14 Sep 2015 01:11:53 +0300
parents c6e90e02dd17
children b0481c071bea
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::AppException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use mro 'c3';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use overload
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 '""' => 'ToString',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 'bool' => sub { return 1; },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 'fallback' => 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 use Carp qw(longmess shortmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use Scalar::Util qw(refaddr);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 use IMPL::Resources::Strings {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 message => "Application exception"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 source => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 callStack => PROP_RO,
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 sub new {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my $instance = $self->next::method(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 $instance->source(shortmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $instance->callStack(longmess);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 return $instance;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 sub ToString {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 return join("\n", $this->message, $this->callStack);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 sub throw {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 die $self->new(@_);
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 C<IMPL::AppException> - исключение приложения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 package MyException;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 'IMPL::AppException' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 level => PROP_RO
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 use IMPL::Resources::Strings {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 message => "Something wrong at level %level%"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 my ($this,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 $this->level($level);
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 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 Для описания собственных исключений в качестве базового класса должен
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 использоваться C<IMPL::AppException> поскольку он позволяет использовать
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 C<IMPL::declare> и объявлять свойства.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 C<IMPL::Exception> также является классом для исключений, однако поскольку
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 он используется в базовых механизмах библиотеки, то в нем не реализованы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 механизмы для описания свойсвт.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 Исключение имеет свойство C<message> которое возвращает текст с описанием
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 проблемы, данное свойство можно реализовать с использованием
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 C<IMPL::Resources::Strings> для реализации поддержки нескольких языков.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 Особенностью тсключений также является то, что при их создании автоматически
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 фиксируется место, где оно было создано и свойства C<source> и C<callStack>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 заполняются автоматически.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 =head2 C<[op]new(@args)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 Оператор создающий новый экземпляр исключения, сначала создает экземпляр
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 исключения, затем заполняет свойства C<source>, C<callStack>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 =head2 C<[op]throw(@args)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 Создает объект исключения и бросает его.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 throw MyException(10);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 MyException->throw(10); # ditto
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 =head2 C<[get]source>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 Строка с описанием в каком файле и где произошло исключение. см. C<Carp>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 =head2 C<[get]callStack>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 Строка со стеком вызовов в момент возникновения исключения. см. C<Carp>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 =head2 C<[get]message>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 Возвращает описание исключения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 =head2 C<ToString()>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 Возвращает текстовое представление, как правило это C<message> и C<callStack>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 =cut