blib/lib/MooseX/Error/Trap.pm

filter: Statementstmt 100% 33 / 33 Branchbran 77.7% 14 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 93.1% 54 / 58 CC 10 SCAR 14.7
CC
10
Cov
92%
__ANON__
6.0
trap
5.0
BEGIN
1.0
CRAP 4.3
↑ Summary
1 package MooseX::Error::Trap;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $MooseX::Error::Trap::VERSION = '0.021';
4 }
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 Moose::Exporter->setup_import_methods(
8 with_caller => [qw{trap}],
9 );
10  
11 # ABSTRACT: Create error traps for methods.
12  
13  
14 =head1 SYNOPSIS
15  
16 Allows you to wrap any method in an eval and specify a dispatch method if the eval trips.
17  
18 package My::Test;
19 use Moose;
20 use MooseX::Error::Trap;
21  
22 trap 'some_method', 'what_to_do';
23 sub some_method {
24 ...
25 }
26  
27 sub what_to_do {
28 my ($self, $error) = @_;
29 ...
30 }
31  
32 =head1 Exported Keyword
33  
34 =head2 trap
35  
36 trap 'wrapped_method', 'trap';
37  
38 Will wrap any calls to 'wrapped_method' in an eval, and if that eval fails then 'trap'
39 is run.
40  
41 Currently 'trap' can be either a string or a CodeRef. The case for a code ref is simple
42 if triped execute the code ref, passing $self and $@. When 'trap' is a string things are
43 a bit more complicated. If 'trap' is the name of an attribute of $self we check to see
44 what the type constraint is on that attr, if it's 'CodeRef' then we grab the value and
45 proceede like a CodeRef. For any other type constraint we return the value of that attr.
46 Lastly if 'trap' is the name of a method ($self->can($trap)) then we execute it passing
47 $@ as the only param.
48  
49 In any other case we just die with $@ as though the eval was not there.
50  
51 =cut
52  
53 4 ▶sub trap {
Subroutine trap: called CC=5 SCAR=16.1
Pod documented
54 4 my $caller = shift;
55 4 my $wrap = shift;
56 4 my $trap = shift;
57  
58 #---------------------------------------------------------------------------
59 # check input
60 #---------------------------------------------------------------------------
61 4 ▶ confess q{No method specified} unless defined $wrap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrap 0 4 4
62 4 ▶ confess q{No trap specified} unless defined $trap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trap 0 4 4
63 4 ▶ if ( ref($trap) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trap eq "") 3 1 4
64 3 ▶ confess sprintf(q{%s can not %s}, $caller, $trap) unless $caller->can($trap);
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->can($trap) 0 3 3
65 }
66  
67 #---------------------------------------------------------------------------
68 # build our trap
69 #---------------------------------------------------------------------------
70 #my $meta = Class::MOP::Class->initialize($caller);
71 4 my $meta = Moose::Meta::Class->initialize($caller);
72 4 my $attr = $meta->get_attribute($trap);
73  
74 $meta->add_around_method_modifier(
75 $wrap,
76 8 ▶ sub{ my $next = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
77 8 my $self = shift;
78  
79 8 my $rv;
80 8 eval { $rv = $self->$next(@_) }
81 8 ▶ or do{
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rv = $self->$next(@_) } } or do { if (my $attr = $meta->get_attribute($trap)) { if ($attr->type_constraint->equals("CodeRef")) { $trap = $self->$trap; } else { return $self->$trap; } ; } ; $rv = ref $trap eq "" ? $self->$trap($@) : (ref $trap eq "CODE" ? &$trap($self, $@) : die($@)) } 4 4 8
82 # If $trap is the name of an attr, and that attr is a CodeRef, grab it
83 4 ▶ if( my $attr = $meta->get_attribute($trap) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $meta->get_attribute($trap)) 2 2 4
84 2 ▶ if ($attr->type_constraint->equals('CodeRef') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->type_constraint->equals('CodeRef')) { } 1 1 2
85 1 $trap = $self->$trap; # grab that code ref and store it to $trap
86 }
87 else {
88 1 return $self->$trap; # non-code attr, just pull the value and use that
89 }
90 }
91 3 ▶ $rv = ref($trap) eq '' ? $self->$trap($@)
Branch: 2 branches
BranchTrueFalseTotal
ref $trap eq '' ? : 1 2 3
ref $trap eq 'CODE' ? : 2 0 2
92 : ref($trap) eq 'CODE' ? $trap->($self,$@)
93 : die $@ ; # sane fall back
94 };
95 7 return $rv;
96 },
97 4 );
98 }
99
100 =head1 TODO
101  
102 =over 4
103  
104 =item * A way to modify the relationship between the method run and the deferment method at runtime.
105  
106 =back
107  
108 =head1 AUTHOR
109  
110 NOTBENH, C<< <NOTBENH at cpan.org> >>
111  
112 =head1 BUGS
113  
114 Please report any bugs or feature requests to C<bug-moosex-error-trap at rt.cpan.org>, or through
115 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Error-Trap>. I will be notified, and then you'll
116 automatically be notified of progress on your bug as I make changes.
117  
118 =head1 SUPPORT
119  
120 You can find documentation for this module with the perldoc command.
121  
122 perldoc MooseX::Error::Trap
123  
124  
125 You can also look for information at:
126  
127 =over 4
128  
129 =item * RT: CPAN's request tracker
130  
131 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Error-Trap>
132  
133 =item * AnnoCPAN: Annotated CPAN documentation
134  
135 L<http://annocpan.org/dist/MooseX-Error-Trap>
136  
137 =item * CPAN Ratings
138  
139 L<http://cpanratings.perl.org/d/MooseX-Error-Trap>
140  
141 =item * Search CPAN
142  
143 L<http://search.cpan.org/dist/MooseX-Error-Trap/>
144  
145 =back
146  
147  
148 =head1 ACKNOWLEDGEMENTS
149  
150 Thanks again to team Moose.
151  
152 =head1 COPYRIGHT & LICENSE
153  
154 Copyright 2009 NOTBENH, all rights reserved.
155  
156 This program is free software; you can redistribute it and/or modify it
157 under the same terms as Perl itself.
158  
159  
160 =cut
161  
162 2 ▶no MooseX::Error::Trap;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 1; # End of MooseX::Error::Trap
↑ Summary