lib/Myriad/Exception/Builder.pm

filter: Statementstmt 88.2% 60 / 68 Branchbran 50.0% 11 / 22 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 12 / 14 Podpod 50.0% 1 / 2 totaltotal 70.8% 90 / 127 CC 17 SCAR 14.8
CC
17
Cov
75%
declare_exception
9.4
create_exception
5.0
__ANON__
4.0
CRAP 4.4
« lib/Myriad/Role.pm ↑ Summary lib/Myriad/Exception.pm »
1 package Myriad::Exception::Builder;
2  
3 22 ▶use Myriad::Class class => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 our $VERSION = '1.001'; # VERSION
6 22 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Myriad::Exception::Builder - applies L<Myriad::Exception::Base> to an exception class
13  
14 =head1 DESCRIPTION
15  
16 See L<Myriad::Exception> for the rĂ´le that defines the exception API.
17  
18 =cut
19  
20 # We deliberately *don't* want class/method keywords, but *do* want MOP
21 22 ▶use Object::Pad qw(:experimental);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 22 ▶use Myriad::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 22 ▶use Myriad::Exception::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 22 ▶use constant CLASS_CREATION_METHOD => Object::Pad::MOP::Class->can('create_class') || Object::Pad::MOP::Class->can('begin_class');
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
'Object::Pad::MOP::Class'->can('create_class') || 'Object::Pad::MOP::Class'->can('begin_class')
l!l&&r!l&&!r
2200
MC/DC0%0/2
'Object::Pad::MOP::Class'->can('create_class') || 'Object::Pad::MOP::Class'->can('begin_class')
'Object::Pad::MOP::Class'->can('create_class') 'Object::Pad::MOP::Class'->can('begin_class')
Truth table33%1/3
A || B
A= 'Object::Pad::MOP::Class'->can('create_class')B= 'Object::Pad::MOP::Class'->can('begin_class')
ABresult
1X1
011
000
27  
28 # Not currently used, but nice as a hint
29 22 our @EXPORT = our @EXPORT_OK = qw(declare_exception);
30  
31 # When importing, you can set the default category to avoid some
32 # repetition when there's a long list of exceptions to be defining
33 22 our %DEFAULT_CATEGORY_FOR_CLASS;
34  
35 22 our %EXCEPTIONS;
36  
37 27 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
38 27 my ($class, %args) = @_;
39 27 my $pkg = caller;
40 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 27 ▶ $DEFAULT_CATEGORY_FOR_CLASS{$pkg} = delete $args{category} if exists $args{category};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"category"} 21 6 27
42 27 ▶ die 'unexpected parameters: ' . join ',', sort keys %args if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 27 27
43 27 *{$pkg . '::declare_exception'} = $class->can('declare_exception');
44 }
45  
46 =head2 declare_exception
47  
48 Creates a new exception under the L<Myriad::Exception> namespace.
49  
50 This will be a class formed from the caller's class:
51  
52 =over 4
53  
54 =item * called from C<Myriad::*>, would strip the C<Myriad::> prefix
55  
56 =item * any other class will remain intact
57  
58 =back
59  
60 e.g. L<Myriad::RPC> when calling this would end up with classes under L<Myriad::Exception::RPC>,
61 but C<SomeCompany::Service::Example> would get L<Myriad::Exception::SomeCompany::Service::Example>
62 as the exception base class.
63  
64 Takes the following parameters:
65  
66 =over 4
67  
68 =item * C<$name> - the exception
69  
70 =item * C<%args> - extra details
71  
72 =back
73  
74 Details can currently include:
75  
76 =over 4
77  
78 =item * C<category>
79  
80 =item * C<message>
81  
82 =back
83  
84 Returns the generated classname.
85  
86 =cut
87  
88 28 ▶sub declare_exception {
Subroutine declare_exception: called CC=8 SCAR=22.4
Pod documented
89 28 my ($name, %args) = @_;
90 28 my $caller = caller;
91 my $pkg = length($name) ? (join '::', (
92 28 ▶ delete($args{package}) || ('Myriad::Exception::' . (caller =~ s{^Myriad::}{}r))
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 25 3 28
Condition50%1/2
delete $args{'package'} || 'Myriad::Exception::' . (caller) =~ s/^Myriad:://ru
l!l
025
MC/DC0%0/1
delete $args{'package'} || 'Myriad::Exception::' . (caller) =~ s/^Myriad:://ru
delete $args{'package'}
Truth table50%1/2
A
A= delete $args{'package'}
Aresult
11
00
93 ), $name) : $caller;
94 28 ▶ my $category = delete $args{category} // $DEFAULT_CATEGORY_FOR_CLASS{$caller};
Condition66%2/3
delete $args{"category"} // $Myriad::Exception::Builder::DEFAULT_CATEGORY_FOR_CLASS{$caller}
l!l&&r!l&&!r
4240
MC/DC0%0/2
delete $args{"category"} // $Myriad::Exception::Builder::DEFAULT_CATEGORY_FOR_CLASS{$caller}
delete $args{"category"} $Myriad::Exception::Builder::DEFAULT_CATEGORY_FOR_CLASS{$caller}
Truth table66%2/3
A // B
A= delete $args{"category"}B= $Myriad::Exception::Builder::DEFAULT_CATEGORY_FOR_CLASS{$caller}
ABresult
1X1
011
000
95 28 ▶ die 'invalid category ' . $category unless $category =~ /^[0-9a-z_]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $category =~ /^[0-9a-z_]+$/u 0 28 28
96 28 ▶ my $message = delete $args{message} // 'unknown';
Condition50%1/2
delete $args{"message"} // "unknown"
l!l
280
MC/DC0%0/1
delete $args{"message"} // "unknown"
delete $args{"message"}
Truth table50%1/2
A
A= delete $args{"message"}
Aresult
11
00
97  
98 28 ▶ die 'already have exception ' . $pkg if exists $EXCEPTIONS{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Myriad::Exception::Builder::EXCEPTIONS{$pkg} 0 28 28
99  
100 28 ▶ $EXCEPTIONS{$pkg} //= create_exception({
Condition33%1/3
$Myriad::Exception::Builder::EXCEPTIONS{$pkg} //= create_exception({"package", $pkg, "category", $category, "message", $message})
l!l&&r!l&&!r
0280
MC/DC0%0/2
$Myriad::Exception::Builder::EXCEPTIONS{$pkg} //= create_exception({"package", $pkg, "category", $category, "message", $message})
$Myriad::Exception::Builder::EXCEPTIONS{$pkg} create_exception({"package", $pkg, "category", $category, "message", $message})
Truth table33%1/3
A //= B
A= $Myriad::Exception::Builder::EXCEPTIONS{$pkg}B= create_exception({"package", $pkg, "category", $category, "message", $message})
ABresult
1X1
011
000
101 package => $pkg,
102 category => $category,
103 message => $message
104 });
105 28 return $pkg;
106 }
107  
108 28 ▶sub create_exception ($details) {
Subroutine create_exception: called CC=4 SCAR=16.0
Pod undocumented
109 28 ▶ my $pkg = delete $details->{package} or die 'no package';
Branch: 1 branch
BranchTrueFalseTotal
my $pkg = delete $details->{"package"} or die "no package" 28 0 28
110 28 ▶ my $category = delete $details->{category} or die 'no category';
Branch: 1 branch
BranchTrueFalseTotal
my $category = delete $details->{"category"} or die "no category" 28 0 28
111 28 ▶ my $message = delete $details->{message} or die 'no message';
Branch: 1 branch
BranchTrueFalseTotal
my $message = delete $details->{"message"} or die "no message" 28 0 28
112  
113 try {
114 28 Myriad::Class->import(
115 target => $pkg,
116 class => '',
117 );
118 28 $EXCEPTIONS{$pkg} = my $class = (CLASS_CREATION_METHOD)->(
119 'Object::Pad::MOP::Class',
120 $pkg,
121 extends => 'Myriad::Exception::Base',
122 );
123 28 $class->add_role('Myriad::Exception');
124 $class->add_method(
125 0 ▶ category => sub ($self) { $category }
Subroutine category: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
126 28 );
127 $class->add_method(
128 2 ▶ message => sub ($self) {
Subroutine __ANON__: called CC=2 SCAR=11.1
129 0 my $str = $message . ' (category=' . $self->category;
130 0 ▶ if($self->reason) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->reason) 0 2 2
if ($self->reason) 0 0 0
131 0 $str .= ' , reason=' . $self->reason;
132 }
133 2 return $str . ')';
134 }
135 28 );
136 { # Until we get class methods in role { } blocks, need to inject this directly
137 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
138 28 ▶ *{$pkg . '::throw'} = sub ($class, @args) {
Subroutine __ANON__: not called CC=2 SCAR=14.0
139 0 ▶ my $self = blessed($class) ? $class : $class->new(@args);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($class) ? : 0 0 0
140 0 die $self;
141 28 };
142 }
143 28 $class->seal;
144 28 return $class;
145 28 } catch ($e) {
146 0 $log->errorf('Failed to raise declare exception - %s', $e);
147 }
148 }
149  
150 22 1;
151  
152 =head1 AUTHOR
153  
154 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
155  
156 See L<Myriad/CONTRIBUTORS> for full details.
157  
158 =head1 LICENSE
159  
160 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
161  
« lib/Myriad/Role.pm ↑ Summary lib/Myriad/Exception.pm »