line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Throwable::Error; |
2
|
|
|
|
|
|
|
# ABSTRACT: an easy-to-use class for error objects |
3
|
|
|
|
|
|
|
$Throwable::Error::VERSION = '1.000'; |
4
|
1
|
|
|
1
|
|
87214
|
use Moo 1.000001; |
|
1
|
|
|
|
|
23
|
|
|
1
|
|
|
|
|
6
|
|
5
|
|
|
|
|
|
|
with 'Throwable', 'StackTrace::Auto'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
8
|
|
|
|
|
|
|
#pod |
9
|
|
|
|
|
|
|
#pod package MyApp::Error; |
10
|
|
|
|
|
|
|
#pod # NOTE: Moo can also be used here instead of Moose |
11
|
|
|
|
|
|
|
#pod use Moose; |
12
|
|
|
|
|
|
|
#pod extends 'Throwable::Error'; |
13
|
|
|
|
|
|
|
#pod |
14
|
|
|
|
|
|
|
#pod has execution_phase => ( |
15
|
|
|
|
|
|
|
#pod is => 'ro', |
16
|
|
|
|
|
|
|
#pod isa => 'MyApp::Phase', |
17
|
|
|
|
|
|
|
#pod default => 'startup', |
18
|
|
|
|
|
|
|
#pod ); |
19
|
|
|
|
|
|
|
#pod |
20
|
|
|
|
|
|
|
#pod ...and in your app... |
21
|
|
|
|
|
|
|
#pod |
22
|
|
|
|
|
|
|
#pod MyApp::Error->throw("all communications offline"); |
23
|
|
|
|
|
|
|
#pod |
24
|
|
|
|
|
|
|
#pod # or... |
25
|
|
|
|
|
|
|
#pod |
26
|
|
|
|
|
|
|
#pod MyApp::Error->throw({ |
27
|
|
|
|
|
|
|
#pod message => "all communications offline", |
28
|
|
|
|
|
|
|
#pod execution_phase => 'shutdown', |
29
|
|
|
|
|
|
|
#pod }); |
30
|
|
|
|
|
|
|
#pod |
31
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
32
|
|
|
|
|
|
|
#pod |
33
|
|
|
|
|
|
|
#pod Throwable::Error is a base class for exceptions that will be thrown to signal |
34
|
|
|
|
|
|
|
#pod errors and abort normal program flow. Throwable::Error is an alternative to |
35
|
|
|
|
|
|
|
#pod L, the features of which are largely |
36
|
|
|
|
|
|
|
#pod provided by the Moo object system atop which Throwable::Error is built. |
37
|
|
|
|
|
|
|
#pod |
38
|
|
|
|
|
|
|
#pod Throwable::Error performs the L and L |
39
|
|
|
|
|
|
|
#pod roles. That means you can call C on it to create and throw an error |
40
|
|
|
|
|
|
|
#pod object in one call, and that every error object will have a stack trace for its |
41
|
|
|
|
|
|
|
#pod creation. |
42
|
|
|
|
|
|
|
#pod |
43
|
|
|
|
|
|
|
#pod =cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use overload |
46
|
1
|
|
|
|
|
7
|
q{""} => 'as_string', |
47
|
1
|
|
|
1
|
|
1736
|
fallback => 1; |
|
1
|
|
|
|
|
1155
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
#pod =attr message |
50
|
|
|
|
|
|
|
#pod |
51
|
|
|
|
|
|
|
#pod This attribute must be defined and must contain a string describing the error |
52
|
|
|
|
|
|
|
#pod condition. This string will be printed at the top of the stack trace when the |
53
|
|
|
|
|
|
|
#pod error is stringified. |
54
|
|
|
|
|
|
|
#pod |
55
|
|
|
|
|
|
|
#pod =cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
has message => ( |
58
|
|
|
|
|
|
|
is => 'ro', |
59
|
|
|
|
|
|
|
isa => Sub::Quote::quote_sub(q{ |
60
|
|
|
|
|
|
|
die "message must be a string" |
61
|
|
|
|
|
|
|
unless defined($_[0]) && !ref($_[0]); |
62
|
|
|
|
|
|
|
}),, |
63
|
|
|
|
|
|
|
required => 1, |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
#pod =attr stack_trace |
67
|
|
|
|
|
|
|
#pod |
68
|
|
|
|
|
|
|
#pod This attribute, provided by L, will contain a stack trace |
69
|
|
|
|
|
|
|
#pod object guaranteed to respond to the C method. For more information |
70
|
|
|
|
|
|
|
#pod about the stack trace and associated behavior, consult the L |
71
|
|
|
|
|
|
|
#pod docs. |
72
|
|
|
|
|
|
|
#pod |
73
|
|
|
|
|
|
|
#pod =method as_string |
74
|
|
|
|
|
|
|
#pod |
75
|
|
|
|
|
|
|
#pod This method will provide a string representing the error, containing the |
76
|
|
|
|
|
|
|
#pod error's message followed by the its stack trace. |
77
|
|
|
|
|
|
|
#pod |
78
|
|
|
|
|
|
|
#pod =cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub as_string { |
81
|
1
|
|
|
1
|
1
|
364
|
my ($self) = @_; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
5
|
my $str = $self->message; |
84
|
1
|
|
|
|
|
26
|
$str .= "\n\n" . $self->stack_trace->as_string; |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
256
|
return $str; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
around BUILDARGS => sub { |
90
|
|
|
|
|
|
|
my $orig = shift; |
91
|
|
|
|
|
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return {} unless @_; |
94
|
|
|
|
|
|
|
return {} if @_ == 1 and ! defined $_[0]; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
if (@_ == 1 and (!ref $_[0]) and defined $_[0] and length $_[0]) { |
97
|
|
|
|
|
|
|
return { message => $_[0] }; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return $self->$orig(@_); |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |