annotate Lib/IMPL/AppException.pm @ 335:e8be9062ecf2

improved resource classes, contracts are deprecated
author cin
date Thu, 13 Jun 2013 20:13:24 +0400
parents 4ddb27ff4a0b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
0a228a35645c added application exception class
sergey
parents:
diff changeset
1 package IMPL::AppException;
0a228a35645c added application exception class
sergey
parents:
diff changeset
2 use strict;
0a228a35645c added application exception class
sergey
parents:
diff changeset
3 use mro 'c3';
0a228a35645c added application exception class
sergey
parents:
diff changeset
4 use overload
0a228a35645c added application exception class
sergey
parents:
diff changeset
5 '""' => 'ToString',
0a228a35645c added application exception class
sergey
parents:
diff changeset
6 'bool' => sub { return 1; },
0a228a35645c added application exception class
sergey
parents:
diff changeset
7 'fallback' => 1;
0a228a35645c added application exception class
sergey
parents:
diff changeset
8
0a228a35645c added application exception class
sergey
parents:
diff changeset
9 use Carp qw(longmess shortmess);
0a228a35645c added application exception class
sergey
parents:
diff changeset
10 use Scalar::Util qw(refaddr);
0a228a35645c added application exception class
sergey
parents:
diff changeset
11
0a228a35645c added application exception class
sergey
parents:
diff changeset
12 use IMPL::Const qw(:prop);
0a228a35645c added application exception class
sergey
parents:
diff changeset
13 use IMPL::Resources::Strings {
278
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
14 message => "Application exception"
253
0a228a35645c added application exception class
sergey
parents:
diff changeset
15 };
0a228a35645c added application exception class
sergey
parents:
diff changeset
16
0a228a35645c added application exception class
sergey
parents:
diff changeset
17 use IMPL::declare {
0a228a35645c added application exception class
sergey
parents:
diff changeset
18 base => [
266
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 261
diff changeset
19 'IMPL::Object' => undef
253
0a228a35645c added application exception class
sergey
parents:
diff changeset
20 ],
0a228a35645c added application exception class
sergey
parents:
diff changeset
21 props => [
0a228a35645c added application exception class
sergey
parents:
diff changeset
22 source => PROP_RO,
0a228a35645c added application exception class
sergey
parents:
diff changeset
23 callStack => PROP_RO,
0a228a35645c added application exception class
sergey
parents:
diff changeset
24 ]
0a228a35645c added application exception class
sergey
parents:
diff changeset
25 };
0a228a35645c added application exception class
sergey
parents:
diff changeset
26
0a228a35645c added application exception class
sergey
parents:
diff changeset
27 sub new {
0a228a35645c added application exception class
sergey
parents:
diff changeset
28 my $self = shift;
0a228a35645c added application exception class
sergey
parents:
diff changeset
29
0a228a35645c added application exception class
sergey
parents:
diff changeset
30 my $instance = $self->next::method(@_);
266
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 261
diff changeset
31
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 261
diff changeset
32 $instance->source(shortmess);
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 261
diff changeset
33 $instance->callStack(longmess);
253
0a228a35645c added application exception class
sergey
parents:
diff changeset
34
0a228a35645c added application exception class
sergey
parents:
diff changeset
35 return $instance;
0a228a35645c added application exception class
sergey
parents:
diff changeset
36 }
0a228a35645c added application exception class
sergey
parents:
diff changeset
37
0a228a35645c added application exception class
sergey
parents:
diff changeset
38 sub ToString {
0a228a35645c added application exception class
sergey
parents:
diff changeset
39 my ($this) = @_;
0a228a35645c added application exception class
sergey
parents:
diff changeset
40
0a228a35645c added application exception class
sergey
parents:
diff changeset
41 return join("\n", $this->message, $this->callStack);
0a228a35645c added application exception class
sergey
parents:
diff changeset
42 }
0a228a35645c added application exception class
sergey
parents:
diff changeset
43
261
sergey
parents: 253
diff changeset
44 sub throw {
sergey
parents: 253
diff changeset
45 my $self = shift;
sergey
parents: 253
diff changeset
46
sergey
parents: 253
diff changeset
47 die $self->new(@_);
sergey
parents: 253
diff changeset
48 }
sergey
parents: 253
diff changeset
49
278
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
50 1;
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
51
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
52 __END__
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
53
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
54 =pod
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
55
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
56 =head1 NAME
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
57
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
58 C<IMPL::AppException> - исключение приложения.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
59
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
60 =head1 SYNOPSIS
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
61
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
62 =begin code
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
63
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
64 package MyException;
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
65 use strict;
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
66
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
67 use IMPL::Const qw(:prop);
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
68 use IMPL::declare {
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
69 base => [
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
70 'IMPL::AppException' => undef
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
71 ],
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
72 props => [
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
73 level => PROP_RO
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
74 ]
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
75 };
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
76
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
77 use IMPL::Resources::Strings {
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
78 message => "Something wrong at level %level%"
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
79 };
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
80
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
81 sub CTOR {
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
82 my ($this,$level) = @_;
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
83
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
84 $this->level($level);
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
85 }
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
86
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
87 =end code
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
88
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
89 =head1 DESCRIPTION
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
90
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
91 Для описания собственных исключений в качестве базового класса должен
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
92 использоваться C<IMPL::AppException> поскольку он позволяет использовать
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
93 C<IMPL::declare> и объявлять свойства.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
94
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
95 C<IMPL::Exception> также является классом для исключений, однако поскольку
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
96 он используется в базовых механизмах библиотеки, то в нем не реализованы
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
97 механизмы для описания свойсвт.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
98
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
99 Исключение имеет свойство C<message> которое возвращает текст с описанием
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
100 проблемы, данное свойство можно реализовать с использованием
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
101 C<IMPL::Resources::Strings> для реализации поддержки нескольких языков.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
102
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
103 Особенностью тсключений также является то, что при их создании автоматически
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
104 фиксируется место, где оно было создано и свойства C<source> и C<callStack>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
105 заполняются автоматически.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
106
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
107 Для исключений переопределены операторы приведения к строке и к булевому
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
108 значению.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
109
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
110 =head1 MEMBERS
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
111
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
112 =head2 C<[op]new(@args)>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
113
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
114 Оператор создающий новый экземпляр исключения, сначала создает экземпляр
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
115 исключения, затем заполняет свойства C<source>, C<callStack>.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
116
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
117 =head2 C<[op]throw(@args)>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
118
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
119 Создает объект исключения и бросает его.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
120
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
121 =begin code
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
122
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
123 throw MyException(10);
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
124 MyException->throw(10); # ditto
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
125
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
126 =end code
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
127
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
128 =head2 C<[get]source>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
129
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
130 Строка с описанием в каком файле и где произошло исключение. см. C<Carp>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
131
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
132 =head2 C<[get]callStack>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
133
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
134 Строка со стеком вызовов в момент возникновения исключения. см. C<Carp>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
135
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
136 =head2 C<[get]message>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
137
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
138 Возвращает описание исключения.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
139
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
140 =head2 C<ToString()>
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
141
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
142 Возвращает текстовое представление, как правило это C<message> и C<callStack>.
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
143
4ddb27ff4a0b core refactoring
cin
parents: 266
diff changeset
144 =cut