Lim/Error.pm

filter: Statementstmt 25.0% 11 / 44 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 21.4% 3 / 14 Podpod 100% 10 / 10 totaltotal 25.5% 24 / 94 CC 12 SCAR 34.0
CC
12
Cov
17%
new
72.0
set
30.0
code
2.0
CRAP 29.8
« Lim/RPC/URIMaps.pm ↑ Summary Lim/Agent/Server.pm »
1 package Lim::Error;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Lim::Error - Encapsulate an error within Lim
13  
14 =head1 VERSION
15  
16 See L<Lim> for version.
17  
18 =cut
19  
20 10 our $VERSION = $Lim::VERSION;
21  
22 =head1 SYNOPSIS
23  
24 =over 4
25  
26 use Lim::Error;
27  
28 $error = Lim::Error->new('This is a simple error');
29  
30 =back
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =item $error = Lim::Error->new(key => value...)
37  
38 Create a new Lim::Error object.
39  
40 =over 4
41  
42 =item code => 500
43  
44 Specify the error code, used in HTTP responses as well as RPC protocols.
45  
46 =item message => $message
47  
48 Specify the error message.
49  
50 =item module => $module
51  
52 Specify the module that created this error, if its a blessed object the L<ref>()
53 of that object will be used.
54  
55 =back
56  
57 =cut
58  
59 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
60 0 my $this = shift;
61 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
62 0 my %args = ( @_ );
63 0 my $self = {
64 code => 500,
65 message => 'Unknown Error',
66 module => 'UNKNOWN'
67 };
68 0 bless $self, $class;
69
70 0 ▶ if (defined $args{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"code"}) 0 0 0
71 0 ▶ unless ($args{code} >= 300 and $args{code} < 600) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"code"} >= 300 and $args{"code"} < 600) 0 0 0
Condition0%0/3
$args{"code"} >= 300 and $args{"code"} < 600
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"code"} >= 300 and $args{"code"} < 600
$args{"code"} >= 300 $args{"code"} < 600
Truth table0%0/3
A and B
A= $args{"code"} >= 300B= $args{"code"} < 600
ABresult
0X0
100
111
72 0 confess __PACKAGE__, ': Invalid code [', $args{code}, '] given in error';
73 }
74 0 $self->{code} = $args{code};
75 }
76 0 ▶ if (defined $args{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"message"}) 0 0 0
77 0 $self->{message} = $args{message};
78 }
79 0 ▶ if (defined $args{module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"module"}) 0 0 0
80 0 ▶ if (blessed($args{module})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args{'module'})) { } 0 0 0
81 0 $self->{module} = ref($args{module});
82 }
83 else {
84 0 $self->{module} = $args{module};
85 }
86 }
87  
88 0 $self;
89 }
90  
91 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
92 }
93  
94 =item $error->set($hash_ref)
95  
96 Populate an error object from a hash reference.
97  
98 =cut
99  
100 0 ▶sub set {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
101 0 ▶ if (ref($_[1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq "HASH") 0 0 0
102 0 ▶ if (exists $_[1]->{'Lim::Error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{"Lim::Error"}) 0 0 0
103 0 foreach (qw(code message module)) {
104 0 ▶ if (exists $_[1]->{'Lim::Error'}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{"Lim::Error"}{$_}) 0 0 0
105 0 $_[0]->{$_} = $_[1]->{'Lim::Error'}->{$_};
106 }
107 }
108 }
109 }
110
111 0 $_[0];
112 }
113  
114 =item $error->code
115  
116 Return the code of the error.
117  
118 =cut
119  
120 0 ▶sub code {
Subroutine code: not called CC=1 SCAR=6.9
Pod documented
121 0 $_[0]->{code};
122 }
123  
124 =item $error->set_code($code)
125  
126 Set the error code to C<$code>.
127  
128 =cut
129  
130 0 ▶sub set_code {
Subroutine set_code: not called CC=1 SCAR=6.9
Pod documented
131 0 $_[0]->{code} = $_[1];
132
133 0 $_[0];
134 }
135  
136 =item $error->message
137  
138 Return the message of the error.
139  
140 =cut
141  
142 0 ▶sub message {
Subroutine message: not called CC=1 SCAR=6.9
Pod documented
143 0 $_[0]->{message};
144 }
145  
146 =item $error->set_message($message)
147  
148 Set the error message to C<$message>
149  
150 =cut
151  
152 0 ▶sub set_message {
Subroutine set_message: not called CC=1 SCAR=6.9
Pod documented
153 0 $_[0]->{message} = $_[1];
154
155 0 $_[0];
156 }
157  
158 =item $error->module
159  
160 Return the module name of the error.
161  
162 =cut
163  
164 0 ▶sub module {
Subroutine module: not called CC=1 SCAR=6.9
Pod documented
165 0 $_[0]->{module};
166 }
167  
168 =item $error->set_module($module_name)
169  
170 Set the module name of the error, this can not take blessed objects.
171  
172 =cut
173  
174 0 ▶sub set_module {
Subroutine set_module: not called CC=1 SCAR=6.9
Pod documented
175 0 $_[0]->{module} = $_[1];
176
177 0 $_[0];
178 }
179  
180 =item $hash_ref = $error->TO_JSON
181  
182 Returns a hash reference describing the error, this is to support passing
183 objects to L<JSON::XS>.
184  
185 =cut
186  
187 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
188 {
189 'Lim::Error' => {
190 code => $_[0]->{code},
191 message => $_[0]->{message},
192 module => $_[0]->{module}
193 }
194 0 };
195 }
196  
197 =item $string = $error->toString
198  
199 Returns a string that describes the error.
200  
201 =cut
202  
203 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
204 0 'Module: ' . $_[0]->{module} . ' Code: ' . $_[0]->{code} . ' Message: ' . $_[0]->{message};
205 }
206  
207 =back
208  
209 =head1 AUTHOR
210  
211 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
212  
213 =head1 BUGS
214  
215 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
216  
217 =head1 SUPPORT
218  
219 You can find documentation for this module with the perldoc command.
220  
221 perldoc Lim::Error
222  
223 You can also look for information at:
224  
225 =over 4
226  
227 =item * Lim issue tracker (report bugs here)
228  
229 L<https://github.com/jelu/lim/issues>
230  
231 =back
232  
233 =head1 ACKNOWLEDGEMENTS
234  
235 =head1 LICENSE AND COPYRIGHT
236  
237 Copyright 2012-2013 Jerry Lundström.
238  
239 This program is free software; you can redistribute it and/or modify it
240 under the terms of either: the GNU General Public License as published
241 by the Free Software Foundation; or the Artistic License.
242  
243 See http://dev.perl.org/licenses/ for more information.
244  
245  
246 =cut
247  
248 10 1; # End of Lim::Error
« Lim/RPC/URIMaps.pm ↑ Summary Lim/Agent/Server.pm »