Object/InsideOut/Exception.pm

filter: Statementstmt 77.0% 67 / 87 Branchbran 70.0% 21 / 30 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 25.0% 1 / 4 totaltotal 74.0% 100 / 135 CC 18 SCAR 17.7
CC
18
Cov
75%
die
9.1
combine
7.5
full_message
5.3
CRAP 5.9
« Object/InsideOut/Dump.pm ↑ Summary Object/InsideOut/Overload.pm »
1 package Object::InsideOut::Exception; {
2  
3 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 44 our $VERSION = '4.05';
7 44 $VERSION = eval $VERSION;
8  
9 # Exceptions generated by this module
10 44 ▶use Exception::Class 1.29 (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 44 'OIO' => {
12 'description' => 'Generic Object::InsideOut exception',
13 # First 3 fields must be: 'Package', 'File', 'Line'
14 'fields' => ['Error', 'Chain'],
15 },
16  
17 'OIO::Code' => {
18 'isa' => 'OIO',
19 'description' =>
20 'Object::InsideOut exception that indicates a coding error',
21 'fields' => ['Info', 'Code'],
22 },
23  
24 'OIO::Internal' => {
25 'isa' => 'OIO::Code',
26 'description' =>
27 'Object::InsideOut exception that indicates a internal problem',
28 'fields' => ['Code', 'Declaration'],
29 },
30  
31 'OIO::Attribute' => {
32 'isa' => 'OIO::Code',
33 'description' =>
34 'Object::InsideOut exception that indicates a coding error',
35 'fields' => ['Attribute'],
36 },
37  
38 'OIO::Method' => {
39 'isa' => 'OIO',
40 'description' =>
41 'Object::InsideOut exception that indicates an method calling error',
42 },
43  
44 'OIO::Args' => {
45 'isa' => 'OIO::Method',
46 'description' =>
47 'Object::InsideOut exception that indicates an argument error',
48 'fields' => ['Usage', 'Arg'],
49 },
50  
51 'OIO::Args::Unhandled' => {
52 'isa' => 'OIO::Args',
53 'description' =>
54 'Object::InsideOut exception that indicates an unhandled argument',
55 'fields' => ['Usage', 'Arg'],
56 },
57  
58 'OIO::Runtime' => {
59 'isa' => 'OIO::Code',
60 'description' =>
61 'Object::InsideOut exception that indicates a runtime error',
62 'fields' => ['Class1', 'Class2'],
63 },
64 44 );
65  
66  
67 # Turn on stack trace by default
68 44 OIO->Trace(1);
69  
70  
71 # A 'throw' method that adds location information to the exception object
72 sub OIO::die
73 80 ▶{
Subroutine die: called CC=7 SCAR=22.1
Pod undocumented
74 80 my $class = shift;
75 80 my %args = @_;
76  
77 # Report on ourself?
78 80 my $report_self = delete($args{'self'});
79  
80 # Ignore ourselves in stack trace, unless told not to
81 80 ▶ if (! $report_self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report_self) 80 0 80
82 80 my @ignore = ('Object::InsideOut::Exception', 'Object::InsideOut');
83 80 ▶ if (exists($args{'ignore_package'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"ignore_package"}) 0 80 80
84 0 ▶ if (ref($args{'ignore_package'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'ignore_package'}) { } 0 0 0
85 0 push(@ignore, @{$args{'ignore_package'}});
86 } else {
87 0 push(@ignore, $args{'ignore_package'});
88 }
89 }
90 80 $args{'ignore_package'} = \@ignore;
91 }
92  
93 # Remove any location information
94 80 my $location = delete($args{'location'});
95  
96 # Create exception object
97 80 my $e = $class->new(%args);
98  
99 # Override location information, if applicable
100 80 ▶ if ($location) {
Branch: 2 branches
BranchTrueFalseTotal
if ($location) { } 30 50 80
elsif ($report_self) { } 0 50 50
101 30 $e->{'package'} = $$location[0];
102 30 $e->{'file'} = $$location[1];
103 30 $e->{'line'} = $$location[2];
104 }
105  
106 # If reporting on ourself, then correct location info
107 elsif ($report_self) {
108 0 my $frame = $e->trace->frame(1);
109 0 $e->{'package'} = $frame->package();
110 0 $e->{'line'} = $frame->line();
111 0 $e->{'file'} = $frame->filename();
112 }
113  
114 # Throw error
115 44 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
116 44 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 80 ▶ if (${$class.'::WARN_ONLY'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$class . '::WARN_ONLY';}) { } 0 80 80
118 0 warn $e->OIO::full_message();
119 } else {
120 80 $e->throw(%args);
121 }
122 }
123  
124  
125 # Provides a fully formatted error message for the exception object
126 sub OIO::full_message
127 43 ▶{
Subroutine full_message: called CC=5 SCAR=16.7
Pod documented
128 43 my $self = shift;
129  
130 # Start with error class and message
131 43 my $msg = ref($self) . ' error: ' . $self->message();
132 43 chomp($msg);
133  
134 # Add fields, if any
135 43 my @fields = $self->Fields();
136 43 foreach my $field (@fields) {
137 176 ▶ next if ($field eq 'Chain');
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "Chain" 43 133 176
138 133 ▶ if (exists($self->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$field}) 23 110 133
139 23 $msg .= "\n$field: " . $self->{$field};
140 23 chomp($msg);
141 }
142 }
143  
144 # Add location
145 43 $msg .= "\nPackage: " . $self->package()
146 . "\nFile: " . $self->file()
147 . "\nLine: " . $self->line();
148  
149 # Chained error messages
150 43 ▶ if (exists($self->{'Chain'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Chain"}) 0 43 43
151 0 my $chain = OIO::full_message($self->{'Chain'});
152 0 chomp($chain);
153 0 $chain =~ s/^/ /mg;
154 0 $msg .= "\n\nSubsequent to the above, the following error also occurred:\n"
155 . $chain;
156 }
157  
158 43 return ($msg . "\n");
159 }
160  
161  
162 # Catch untrapped errors
163 # Usage: local $SIG{'__DIE__'} = 'OIO::trap';
164 sub OIO::trap
165 5 ▶{
Subroutine trap: called CC=2 SCAR=8.1
Pod undocumented
166 # Just rethrow if already an exception object
167 5 ▶ if (Object::InsideOut::Util::is_it($_[0], 'Exception::Class::Base')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Object::InsideOut::Util::is_it($_[0], "Exception::Class::Base")) 0 5 5
168 0 die($_[0]);
169 }
170  
171 # Package the error into an object
172 OIO->die(
173 5 'location' => [ caller() ],
174 'message' => 'Trapped uncaught error',
175 'Error' => join('', @_));
176 }
177  
178  
179 # Combine errors into a single error object
180 sub OIO::combine
181 49 ▶{
Subroutine combine: called CC=7 SCAR=20.2
Pod undocumented
182 49 my ($err1, $err2) = @_;
183  
184 # Massage second error, if needed
185 49 ▶ if ($err2 && ! ref($err2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err2 and not ref $err2) 0 49 49
Condition66%2/3
$err2 and not ref $err2
!ll&&!rl&&r
4360
MC/DC0%0/2
$err2 and not ref $err2
$err2 not ref $err2
Truth table66%2/3
A and not(B)
A= $err2B= not ref $err2
ABresult
0X0
100
111
186 0 my $e = OIO->new(
187 'message' => "$err2",
188 'ignore_package' => [ 'Object::InsideOut::Exception' ]
189 );
190  
191 0 my $frame = $e->trace->frame(1);
192 0 $e->{'package'} = $frame->package();
193 0 $e->{'line'} = $frame->line();
194 0 $e->{'file'} = $frame->filename();
195  
196 0 $err2 = $e;
197 }
198  
199 # Massage first error, if needed
200 49 ▶ if ($err1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err1) { } 42 7 49
201 42 ▶ if (! ref($err1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $err1) 2 40 42
202 2 my $e = OIO->new(
203 'message' => "$err1",
204 'ignore_package' => [ 'Object::InsideOut::Exception' ]
205 );
206  
207 2 my $frame = $e->trace->frame(1);
208 2 $e->{'package'} = $frame->package();
209 2 $e->{'line'} = $frame->line();
210 2 $e->{'file'} = $frame->filename();
211  
212 2 $err1 = $e;
213 }
214  
215 # Combine errors, if possible
216 42 ▶ if ($err2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err2) 2 40 42
217 2 ▶ if (Object::InsideOut::Util::is_it($err1, 'OIO')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Object::InsideOut::Util::is_it($err1, 'OIO')) { } 1 1 2
218 1 $err1->{'Chain'} = $err2;
219 } else {
220 1 warn($err2); # Can't combine
221 }
222 }
223  
224 } else {
225 7 $err1 = $err2;
226 7 undef($err2);
227 }
228  
229 49 return ($err1);
230 }
231  
232 } # End of package's lexical scope
233  
234 44 1;
« Object/InsideOut/Dump.pm ↑ Summary Object/InsideOut/Overload.pm »