Lim/Error.pm

filter: Statementstmt 25.5% 11 / 43 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 21.4% 3 / 14 Podpod 100% 10 / 10 totaltotal 26.3% 24 / 91 SCAR 43.7
CC
11
Cov
17%
new
56.0
set
30.0
code
2.0
CRAP 79.0
« Lim/RPC/URIMaps.pm ↑ Summary Lim/RPC/Value.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 sub new {
60 0 ▶ my $this = shift;
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
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 => 'Generic 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 $self->{message} = $args{message};
77 0 ▶ if (defined $args{module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"module"}) 0 0 0
78 0 ▶ if (blessed($args{module})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args{'module'})) { } 0 0 0
79 0 $self->{module} = ref($args{module});
80 }
81 else {
82 0 $self->{module} = $args{module};
83 }
84 }
85  
86 0 $self;
87 }
88  
89 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
90 }
91  
92 =item $error->set($hash_ref)
93  
94 Populate an error object from a hash reference.
95  
96 =cut
97  
98 sub set {
99 0 ▶ if (ref($_[1]) eq 'HASH') {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq "HASH") 0 0 0
100 0 ▶ if (exists $_[1]->{'Lim::Error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{"Lim::Error"}) 0 0 0
101 0 foreach (qw(code message module)) {
102 0 ▶ if (exists $_[1]->{'Lim::Error'}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{"Lim::Error"}{$_}) 0 0 0
103 0 $_[0]->{$_} = $_[1]->{'Lim::Error'}->{$_};
104 }
105 }
106 }
107 }
108
109 0 $_[0];
110 }
111  
112 =item $error->code
113  
114 Return the code of the error.
115  
116 =cut
117  
118 sub code {
119 0 ▶ $_[0]->{code};
Subroutine code: not called CC=1 SCAR=6.9
Pod documented
120 }
121  
122 =item $error->set_code($code)
123  
124 Set the error code to C<$code>.
125  
126 =cut
127  
128 sub set_code {
129 0 ▶ $_[0]->{code} = $_[1];
Subroutine set_code: not called CC=1 SCAR=6.9
Pod documented
130
131 0 $_[0];
132 }
133  
134 =item $error->message
135  
136 Return the message of the error.
137  
138 =cut
139  
140 sub message {
141 0 ▶ $_[0]->{message};
Subroutine message: not called CC=1 SCAR=6.9
Pod documented
142 }
143  
144 =item $error->set_message($message)
145  
146 Set the error message to C<$message>
147  
148 =cut
149  
150 sub set_message {
151 0 ▶ $_[0]->{message} = $_[1];
Subroutine set_message: not called CC=1 SCAR=6.9
Pod documented
152
153 0 $_[0];
154 }
155  
156 =item $error->module
157  
158 Return the module name of the error.
159  
160 =cut
161  
162 sub module {
163 0 ▶ $_[0]->{module};
Subroutine module: not called CC=1 SCAR=6.9
Pod documented
164 }
165  
166 =item $error->set_module($module_name)
167  
168 Set the module name of the error, this can not take blessed objects.
169  
170 =cut
171  
172 sub set_module {
173 0 ▶ $_[0]->{module} = $_[1];
Subroutine set_module: not called CC=1 SCAR=6.9
Pod documented
174
175 0 $_[0];
176 }
177  
178 =item $hash_ref = $error->TO_JSON
179  
180 Returns a hash reference describing the error, this is to support passing
181 objects to L<JSON::XS>.
182  
183 =cut
184  
185 sub TO_JSON {
186 {
187 'Lim::Error' => {
188 code => $_[0]->{code},
189 message => $_[0]->{message},
190 module => $_[0]->{module}
191 }
192 0 ▶ };
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
193 }
194  
195 =item $string = $error->toString
196  
197 Returns a string that describes the error.
198  
199 =cut
200  
201 sub toString {
202 0 ▶ 'Module: ', $_[0]->{module}, ' Code: ', $_[0]->{code}, ' Message: ', $_[0]->{message};
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
203 }
204  
205 =back
206  
207 =head1 AUTHOR
208  
209 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
210  
211 =head1 BUGS
212  
213 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
214  
215 =head1 SUPPORT
216  
217 You can find documentation for this module with the perldoc command.
218  
219 perldoc Lim::Error
220  
221 You can also look for information at:
222  
223 =over 4
224  
225 =item * Lim issue tracker (report bugs here)
226  
227 L<https://github.com/jelu/lim/issues>
228  
229 =back
230  
231 =head1 ACKNOWLEDGEMENTS
232  
233 =head1 LICENSE AND COPYRIGHT
234  
235 Copyright 2012-2013 Jerry Lundström.
236  
237 This program is free software; you can redistribute it and/or modify it
238 under the terms of either: the GNU General Public License as published
239 by the Free Software Foundation; or the Artistic License.
240  
241 See http://dev.perl.org/licenses/ for more information.
242  
243  
244 =cut
245  
246 10 1; # End of Lim::Error
« Lim/RPC/URIMaps.pm ↑ Summary Lim/RPC/Value.pm »