blib/lib/Glitch.pm

filter: Statementstmt 95.1% 118 / 124 Branchbran 76.9% 40 / 52 Conditioncond 78.9% 30 / 38 MC/DCmcdc 63.1% 12 / 19 Subroutinesub 87.5% 28 / 32 Podpod 16.6% 1 / 6 totaltotal 84.5% 229 / 271 CC 63 SCAR 15.4
CC
63
Cov
88%
_stack
12.6
build_meta
10.4
_build_glitch
8.0
CRAP 4.6
↑ Summary
1 package Glitch;
2 7 our $VERSION = '0.11';
3 8 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Data::Dumper; use feature qw/state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 state (%META);
6  
7 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 $Data::Dumper::Deparse = 1;
9 }
10  
11 11 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
12 11 my ($pkg, %import) = @_;
13 11 build_meta(%import);
14 11 do {
15 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 my $package = caller();
17 11 *{"${package}::glitch"} = \&glitch;
18 };
19 }
20  
21 11 ▶sub build_meta {
Subroutine build_meta: called CC=10 SCAR=23.4
Pod undocumented
22 11 my (%import) = @_;
23 11 ▶ if (keys %import) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %import) 5 6 11
24 5 ▶ $META{config_parser} = delete $import{glitch_config_parser} if defined $import{glitch_config_parser};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_config_parser"} 1 4 5
25 5 ▶ %import = (_parse_config(delete $import{glitch_config}), %import) if (defined $import{glitch_config});
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_config"} 3 2 5
26 5 ▶ if (defined $import{glitch_logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $import{"glitch_logger"}) 4 1 5
27 4 $META{logger} = delete $import{glitch_logger};
28 4 $META{logger_enabled} = !!1;
29 }
30 5 for (qw/glitch_logger_enabled glitch_logger_format glitch_logger_method glitch_stringify_format/) {
31 20 ▶ if (defined $import{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $import{$_}) 4 16 20
32 4 (my $name = $_) =~ s/^glitch_//;
33 4 $META{$name} = delete $import{$_};
34 }
35 }
36 0 ▶ $META{stringify} = { %{$META{stringify} || {}}, %{ delete $import{glitch_stringify} } }
Condition0%0/2
$META{'stringify'} || {}
l!l
00
MC/DC0%0/1
$META{'stringify'} || {}
$META{'stringify'}
Truth table0%0/2
A
A= $META{'stringify'}
Aresult
11
00
37 5 ▶ if defined $import{glitch_stringify};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_stringify"} 0 5 5
38 _build_glitch(
39 name => $_,
40 6 %{ $import{$_} },
41 30 map { $_ => '' } ("file", "filepath", "line", "stacktrace", "module")
42 5 ) for sort keys %import;
43 }
44 11 return 1;
45 }
46  
47 7 ▶sub glitch {
Subroutine glitch: called CC=2 SCAR=6.9
Pod documented
48 7 my %options = (
49 name => shift,
50 _stack(),
51 @_
52 );
53 7 ▶ _build_glitch(%options) if (!$META{glitches}{$options{name}});
Branch: 1 branch
BranchTrueFalseTotal
unless $META{"glitches"}{$options{"name"}} 1 6 7
54 7 die _log($META{glitches}{$options{name}}->new(%options));
55 }
56  
57 0 ▶sub logger {
Subroutine logger: not called CC=1 SCAR=6.9
Pod undocumented
58 0 $META{logger} = $_[0];
59 }
60  
61 0 ▶sub logger_enabled {
Subroutine logger_enabled: not called CC=1 SCAR=6.9
Pod undocumented
62 0 $META{logger_enabled} = !!$_[0];
63 }
64  
65 0 ▶sub logger_format {
Subroutine logger_format: not called CC=1 SCAR=6.9
Pod undocumented
66 0 $META{logger_format} = $_[0];
67 }
68  
69 0 ▶sub logger_method {
Subroutine logger_method: not called CC=1 SCAR=6.9
Pod undocumented
70 0 $META{logger_method} = $_[0];
71 }
72  
73 7 ▶sub _log {
Subroutine _log: called CC=6 SCAR=18.7
74 7 ▶ if ($META{logger_enabled} && $META{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($META{"logger_enabled"} and $META{"logger"}) 5 2 7
Condition66%2/3
$META{"logger_enabled"} and $META{"logger"}
!ll&&!rl&&r
205
MC/DC50%1/2
$META{"logger_enabled"} and $META{"logger"}
$META{"logger_enabled"} $META{"logger"}
Truth table66%2/3
A and B
A= $META{"logger_enabled"}B= $META{"logger"}
ABresult
0X0
100
111
75 5 my $ref = ref $META{logger};
76 5 ▶ my $glitch = $META{logger_format} ? $_[0]->stringify($META{logger_format}) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$META{'logger_format'} ? : 5 0 5
77 5 ▶ my $cb = $META{logger_method} || 'err';
Condition50%1/2
$META{'logger_method'} || 'err'
l!l
05
MC/DC0%0/1
$META{'logger_method'} || 'err'
$META{'logger_method'}
Truth table50%1/2
A
A= $META{'logger_method'}
Aresult
11
00
78 5 ▶ ($ref eq 'CODE') ? $META{logger}->($glitch) : $META{logger}->$cb($glitch);
Branch: 1 branch
BranchTrueFalseTotal
$ref eq 'CODE' ? : 5 0 5
79 }
80 7 return $_[0];
81 }
82  
83  
84 7 ▶sub _build_glitch {
Subroutine _build_glitch: called CC=8 SCAR=20.8
85 7 my (%options) = @_;
86 7 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 7 ▶ my $class = sprintf q|%s::%s|, $options{object_name} ||= 'Glitch', $options{name};
Condition50%1/2
$options{"object_name"} ||= "Glitch"
l!l
07
MC/DC0%0/1
$options{"object_name"} ||= "Glitch"
$options{"object_name"}
Truth table50%1/2
A
A= $options{"object_name"}
Aresult
11
00
88 7 ▶ my @methods = map { my $struct = $_ =~ m/(file|filepath|line|stacktrace|module)/ ? "''" : _stringify_struct($options{$_}); "sub $_ { return \$_[0]->{$_} || $struct; }" } sort keys %options;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /(file|filepath|line|stacktrace|module)/ ? : 35 21 56
89 7 unshift @methods, 'sub new { my $self = shift; return bless {@_}, $self; }';
90 7 ▶ my $format = $META{stringify_format} ||= 'default';
Condition100%2/2
$META{"stringify_format"} ||= "default"
l!l
16
MC/DC100%1/1
$META{"stringify_format"} ||= "default"
$META{"stringify_format"}
Truth table100%2/2
A
A= $META{"stringify_format"}
Aresult
11
00
91 7 push @methods, 'sub keys { my $keys = ' . _stringify_struct([sort keys %options]) . '; return wantarray ? @{ $keys } : $keys; }';
92 7 push @methods, 'sub hash { my %hash; $hash{$_} = $_[0]->$_ for ( @{ $_[0]->keys } ); return \%hash }';
93 7 push @methods, 'sub stringify { my $type = sprintf "stringify_%s", ($_[1] || "' . $format . '"); return $_[0]->$type(); }';
94 7 ▶ $META{stringify}{default} ||= 'return $_[0]->message . " at " . $_[0]->filepath . " line " . $_[0]->line . "\n";';
Condition100%2/2
$META{"stringify"}{"default"} ||= "return \$_[0]->message . \" at \" . \$_[0]->filepath . \" line \" . \$_[0]->line . \"\\n\";"
l!l
16
MC/DC100%1/1
$META{"stringify"}{"default"} ||= "return \$_[0]->message . \" at \" . \$_[0]->filepath . \" line \" . \$_[0]->line . \"\\n\";"
$META{"stringify"}{"default"}
Truth table100%2/2
A
A= $META{"stringify"}{"default"}
Aresult
11
00
95 7 ▶ $META{stringify}{json} ||= 'require JSON; JSON->new->encode($_[0]->hash);';
Condition100%2/2
$META{"stringify"}{"json"} ||= "require JSON; JSON->new->encode(\$_[0]->hash);"
l!l
16
MC/DC100%1/1
$META{"stringify"}{"json"} ||= "require JSON; JSON->new->encode(\$_[0]->hash);"
$META{"stringify"}{"json"}
Truth table100%2/2
A
A= $META{"stringify"}{"json"}
Aresult
11
00
96 7 push @methods, sprintf "sub stringify_%s { %s }", $_, $META{stringify}{$_} for sort keys %{$META{stringify}};
97 7 my $package = sprintf(q|package %s;
98 use overload '""' => \&stringify;
99 %s
100 1;|, $class, join( "\n", @methods) );
101 7 ▶ eval $package;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine file: called CC=2 SCAR=6.9
Subroutine filepath: called CC=2 SCAR=6.9
Subroutine hash: called CC=2 SCAR=6.9
Subroutine keys: called CC=2 SCAR=6.9
Subroutine line: called CC=2 SCAR=6.9
Subroutine message: called CC=2 SCAR=6.9
Subroutine module: called CC=2 SCAR=6.9
Subroutine name: called CC=2 SCAR=6.9
Subroutine new: called CC=1 SCAR=0.0
Subroutine object_name: called CC=2 SCAR=6.9
Subroutine stacktrace: called CC=2 SCAR=6.9
Condition91%11/12
$_[0]{'file'} || ''
l!l
42
$_[0]{'filepath'} || ''
l!l
90
$_[0]{'line'} || ''
l!l
24
$_[0]{'message'} || 'this is a test'
l!l
41
$_[0]{'module'} || ''
l!l
55
$_[0]{'name'} || 'one'
l!l
11
MC/DC100%1/1
$_[0]{'file'} || ''
$_[0]{'file'}
MC/DC0%0/1
$_[0]{'filepath'} || ''
$_[0]{'filepath'}
MC/DC100%1/1
$_[0]{'line'} || ''
$_[0]{'line'}
MC/DC100%1/1
$_[0]{'message'} || 'this is a test'
$_[0]{'message'}
MC/DC100%1/1
$_[0]{'module'} || ''
$_[0]{'module'}
MC/DC100%1/1
$_[0]{'name'} || 'one'
$_[0]{'name'}
Truth table100%2/2
A
A= $_[0]{'file'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'filepath'}
Aresult
11
00
Truth table100%2/2
A
A= $_[0]{'line'}
Aresult
11
00
Truth table100%2/2
A
A= $_[0]{'message'}
Aresult
11
00
Truth table100%2/2
A
A= $_[0]{'module'}
Aresult
11
00
Truth table100%2/2
A
A= $_[0]{'name'}
Aresult
11
00
102 7 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
103 7 $META{glitches}{$options{name}} = $class;
104 7 return 1;
105 }
106  
107 32 ▶sub _stringify_struct {
Subroutine _stringify_struct: called CC=3 SCAR=11.0
108 32 my ( $struct ) = @_;
109 38 ▶ return 'undefined' unless defined $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $struct 0 32 32
110 34 ▶ $struct = ref $struct ? Dumper $struct : "'$struct'";
Branch: 1 branch
BranchTrueFalseTotal
ref $struct ? : 7 22 29
111 32 $struct =~ s/\$VAR1 = //;
112 32 $struct =~ s/\s*\n*\s*package Glitch\;|use warnings\;|use strict\;//g;
113 32 $struct =~ s/{\s*\n*/{/;
114 33 $struct =~ s/;$//;
115 33 return $struct;
116 }
117  
118 17 ▶sub _stack {
Subroutine _stack: called CC=12 SCAR=25.4
119 8 my @caller; my $i = 0; my @stack;
120 13 ▶ while(@caller = caller($i++)){
Branch: 1 branch
BranchTrueFalseTotal
if (@caller = caller $i++) 21 7 28
121 30 ▶ next if $caller[0] eq 'Glitch';
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] eq "Glitch" 7 14 21
122 22 $stack[$i+1]->{module} = $caller[0];
123 20 $stack[$i+1]->{filepath} = $caller[1];
124 19 ▶ $stack[$i+1]->{file} = $1 if $caller[1] =~ /([^\/]+)$/;;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[1] =~ m[([^/]+)$] 14 0 14
125 20 ▶ $stack[$i+1]->{line} = $1 if $caller[2] =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[2] =~ /(\d+)/ 14 0 14
126 24 ▶ $stack[$i]->{sub} = $1 if $caller[3] =~ /([^:]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[3] =~ /([^:]+)$/ 14 0 14
127 }
128 16 my $msg = $stack[-1];
129 $msg->{stacktrace} = join '->', reverse map {
130 21 ▶ my $module = $_->{module} !~ m/^main$/ ? $_->{module} : $_->{file};
Branch: 1 branch
BranchTrueFalseTotal
!($_->{'module'} =~ /^main$/) ? : 0 14 14
131 $_->{sub}
132 ? $module . '::' . $_->{sub} . ':' . $_->{line}
133 : $module . ':' . $_->{line}
134 18 ▶ } grep {
Branch: 1 branch
BranchTrueFalseTotal
$_->{'sub'} ? : 7 7 14
135 14 $_ && $_->{module} && $_->{line} && $_->{file}
136 36 ▶ } @stack;
Condition77%7/9
$_ && $_->{'module'} && $_->{'line'} && $_->{'file'}
!ll&&!rl&&r
25014
$_ and $_->{"module"} and $_->{"line"}
!ll&&!rl&&r
31014
$_ and $_->{"module"}
!ll&&!rl&&r
25714
MC/DC50%2/4
$_ && $_->{'module'} && $_->{'line'} && $_->{'file'}
$_ $_->{"module"} $_->{"line"} $_->{'file'}
Truth table60%3/5
A and B and C && D
A= $_B= $_->{"module"}C= $_->{"line"}D= $_->{'file'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
137 8 ▶ delete $msg->{stacktrace} unless $msg->{stacktrace};
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->{"stacktrace"} 0 7 7
138 7 return %{$msg};
139 }
140  
141 11 ▶sub _parse_config {
Subroutine _parse_config: called CC=6 SCAR=17.9
142 5 my ($config, %out) = @_;
143 5 ▶ if (ref $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config) 1 4 5
144 map {
145 2 %out = _parse_config($_, %out)
146 1 } @{$config};
147 1 return %out;
148 }
149 4 ▶ open my $fh, '<', $config or glitch('glitchInternal1', message => 'Cannot open file for writing', error => $!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $config or glitch("glitchInternal1", "message", "Cannot open file for writing", "error", $!) 4 0 4
150 4 my $content = do { local $/; <$fh> };
151 4 close $fh;
152 4 ▶ $content = $META{config_parser} ? $META{config_parser}->($content) : eval $content;
Branch: 1 branch
BranchTrueFalseTotal
$META{'config_parser'} ? : 1 3 4
153 4 ▶ glitch("glitchInternal2", message => 'Cannot parse file', error => $@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
154 4 ▶ return (%out, %{$content || {}});
Condition100%2/2
$content || {}
l!l
69
MC/DC100%1/1
$content || {}
$content
Truth table100%2/2
A
A= $content
Aresult
11
00
155 }
156  
157 =head1 NAME
158  
159 Glitch - Exception Handling.
160  
161 =head1 VERSION
162  
163 Version 0.11
164  
165 =cut
166  
167 =head1 SYNOPSIS
168  
169 package Foo;
170  
171 use Glitch;
172  
173 sub bar {
174 do { ... } or glitch('one', message => 'Create a new glitch error message');
175  
176 ... later in your code you can then reuse glitch 'one'
177  
178 do { ... } or glitch('one');
179 }
180  
181 ...
182  
183 package Foo;
184  
185 use Glitch (
186 one => {
187 message => 'Create a new glitch error message'
188 },
189 two => {
190 message => 'A different glitch error message',
191 fileName => '',
192 }
193 );
194  
195 sub bar {
196 eval {
197 do { ... } or glitch('one');
198 ...
199 do { ... } or glitch('two', fileName => 'abc');
200 };
201 if ($@) {
202 do { ... } if $@->name eq 'one';
203 do { ... } if $@->name eq 'two';
204 }
205 }
206  
207 1;
208  
209 ...
210  
211 package Glitches;
212  
213 use Glitch (
214 glitch_logger => sub {
215 open my $fh, '>', 'glitch.log';
216 print $fh $_[0] . "\n";
217 close $fh;
218 },
219 glitch_stringify_format => 'json',
220 one => {
221 message => 'Create a new glitch error message'
222 },
223 two => {
224 message => 'A different glitch error message',
225 fileName => '',
226 }
227 );
228  
229 1;
230  
231 package Foo;
232  
233 use Glitch;
234 use Glitches;
235  
236 sub bar {
237 eval {
238 do { ... } or glitch('one');
239 ...
240 do { ... } or glitch('two', fileName => 'abc');
241 };
242 if ($@) {
243 if ($@->name eq 'one') { ... }
244 elsif ($@->name eq 'two') { ... }
245 }
246 }
247  
248 1;
249  
250 ...
251  
252 # glitch.conf
253 {
254 "one": {
255 "message": "this is a error message"
256 },
257 "two": {
258 "message": "this is another error messsage"
259 }
260 }
261  
262 package Foo;
263 use JSON;
264 use Glitch (
265 glitch_config => 'glitch.conf'
266 glitch_config_parser => sub {
267 JSON->new->decode($_[0]);
268 }
269 );
270  
271 sub bar {
272 eval {
273 do { ... } or glitch('one');
274 ...
275 do { ... } or glitch('two');
276 };
277 if ($@) {
278 if ($@->name eq 'one') { ... }
279 elsif ($@->name eq 'two') { ... }
280 }
281 }
282  
283 =head1 EXPORT
284  
285 =head2 glitch
286  
287 =cut
288  
289 =head1 AUTHOR
290  
291 LNATION, C<< <email at lnation.org> >>
292  
293 =head1 BUGS
294  
295 Please report any bugs or feature requests to C<bug-glitch at rt.cpan.org>, or through
296 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Glitch>. I will be notified, and then you'll
297 automatically be notified of progress on your bug as I make changes.
298  
299 =head1 SUPPORT
300  
301 You can find documentation for this module with the perldoc command.
302  
303 perldoc Glitch
304  
305 You can also look for information at:
306  
307 =over 4
308  
309 =item * RT: CPAN's request tracker (report bugs here)
310  
311 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Glitch>
312  
313 =item * CPAN Ratings
314  
315 L<https://cpanratings.perl.org/d/Glitch>
316  
317 =item * Search CPAN
318  
319 L<https://metacpan.org/release/Glitch>
320  
321 =back
322  
323 =head1 ACKNOWLEDGEMENTS
324  
325 =head1 LICENSE AND COPYRIGHT
326  
327 This software is Copyright (c) 2022 by LNATION.
328  
329 This is free software, licensed under:
330  
331 The Artistic License 2.0 (GPL Compatible)
332  
333 =cut
334  
335 7 1; # End of Glitch
↑ Summary