blib/lib/Froody/Response/Error.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 50.0% 7 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod 81.8% 9 / 11 totaltotal 77.9% 92 / 118 CC 10 SCAR 7.8
CC
10
Cov
78%
from_exception
4.1
set_error
3.4
as_terse
3.0
CRAP 2.2
« blib/lib/Froody/Base.pm ↑ Summary blib/lib/Froody/Plugin.pm »
1 package Froody::Response::Error;
2 33 ▶use base qw(Froody::Response);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 ▶use Froody::Response::Terse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 my $logger = get_logger("froody.response.error");
12  
13 =head1 NAME
14  
15 Froody::Response::Error - create a response from an error
16  
17 =head1 SYNOPSIS
18  
19 # from known problems
20 my $response = Froody::Response::Error->from_exception($@, $repository)
21  
22 print $response->render;
23  
24 # or
25
26 $response->throw;
27
28 =head1 DESCRIPTION
29  
30 This class is designed to allow you to create error responses quickly
31 and easily.
32  
33 =cut
34  
35 # from_exception is documented
36 9 ▶sub from_exception {
Subroutine from_exception: called CC=3 SCAR=14.2
Pod undocumented
37 9 my ($class, $error, $repository) = @_;
38  
39 9 ▶ unless (blessed($error) && $error->isa("Froody::Error")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($error) and $error->isa("Froody::Error")) 0 9 9
Condition33%1/3
builtin::blessed($error) and $error->isa("Froody::Error")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Froody::Error")
builtin::blessed($error) $error->isa("Froody::Error")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("Froody::Error")
ABresult
0X0
100
111
40 0 $logger->error("unknown error of type ".ref($error)." thrown: $error");
41 0 $logger->error( $error );
42 0 $error = Froody::Error->new('froody.error.unknown');
43 }
44  
45 9 my $structure = $repository->get_closest_errortype($error->code);
46 9 return $class->new->set_error( $error )->structure( $structure );
47 }
48  
49 # set_error is documented
50 sub set_error
51 14 ▶{
Subroutine set_error: called CC=3 SCAR=12.3
Pod undocumented
52 14 my $self = shift;
53 14 my $err = shift;
54  
55 # make sure it's a Froody Error
56 14 ▶ if (blessed($err) && $err->isa("Froody::Error"))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($err) and $err->isa('Froody::Error')) { } 14 0 14
Condition33%1/3
builtin::blessed($err) and $err->isa("Froody::Error")
!ll&&!rl&&r
0014
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Froody::Error")
builtin::blessed($err) $err->isa("Froody::Error")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Froody::Error")
ABresult
0X0
100
111
57 {
58 14 $self->{error} = $err
59 }
60 else
61 {
62 0 $self->{error} = Froody::Error->new("unknown", "$err");
63 }
64  
65 14 return $self;
66 }
67  
68 =head1 ACCESSORS
69  
70 =over
71  
72 =item code (read only)
73  
74 =item message (read only)
75  
76 =item data (read_only)
77  
78 =item status (read only)
79  
80 =back
81  
82 =cut
83  
84 sub code
85 1 ▶{
Subroutine code: called CC=2 SCAR=7.6
Pod documented
86 1 my $self = shift;
87 0 ▶ if (@_) { Froody::Error->throw("perl.methodcall.param", "code takes no arguments") }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
88 1 $self->{error}->code;
89 }
90  
91 sub message
92 6 ▶{
Subroutine message: called CC=2 SCAR=7.6
Pod documented
93 6 my $self = shift;
94 0 ▶ if (@_) { Froody::Error->throw("perl.methodcall.param", "message takes no arguments") }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
95 6 $self->{error}->message;
96 }
97  
98 sub data
99 5 ▶{
Subroutine data: called CC=2 SCAR=7.6
Pod documented
100 5 my $self = shift;
101 0 ▶ if (@_) { Froody::Error->throw("perl.methodcall.param", "data takes no arguments") }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
102 5 $self->{error}->data;
103 }
104  
105 #####
106  
107 7 ▶sub Froody::Response::as_error {
Subroutine as_error: called CC=1 SCAR=0.0
Pod documented
108 7 my $self = shift->as_terse;
109
110 # TODO: Error checking
111
112 # okay, now from the terse, build an error
113 5 my $content = $self->content;
114 5 my $code = delete $content->{code};
115 5 my $msg = delete $content->{msg};
116 5 my $error = Froody::Error->new($code, $msg, $content);
117  
118 5 return Froody::Response::Error->new()
119 ->set_error($error)
120 ->structure($self->structure);
121 }
122  
123 0 ▶sub as_error { $_[0] }
Subroutine as_error: not called CC=1 SCAR=6.9
Pod documented
124  
125 sub as_terse
126 2 ▶{
Subroutine as_terse: called CC=3 SCAR=11.1
Pod documented
127 2 my $self = shift;
128
129 2 my $error = $self->{error};
130
131 # make this into the right data structure
132 2 my $data = $error->data;
133 2 ▶ $data = {} unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 2 2
134 2 ▶ $data = { -text => $data } unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 2 2
135
136 # add the extra keys
137 2 $data->{msg} = $error->message;
138 2 $data->{code} = $error->code;
139
140 2 my $terse = Froody::Response::Terse->new
141 ->status("fail")
142 ->content($data)
143 ->structure($self->structure);
144  
145 2 return $terse;
146 }
147  
148 1 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
149 1 my $self = shift;
150 1 $self->as_terse->render();
151 }
152  
153 # we always fail
154  
155 2 ▶sub status { "fail" }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
156  
157 =head2 throw
158  
159 Throw the response as an L<Froody::Error> if possible. It may throw C<undef> if no
160 error is associated with the current response.
161  
162 =cut
163  
164 sub throw
165 6 ▶{
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
166 6 my $self = shift;
167 6 die $self->{error}; # might die with undef. So be it
168 }
169  
170 =head1 BUGS
171  
172 This is missing the C<argument> functionality from the original
173 implementation of Froody::Response.
174  
175 Please report any bugs you find via the CPAN RT system.
176 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
177  
178 =head1 AUTHOR
179  
180 Copyright Fotango 2005. All rights reserved.
181  
182 Please see the main L<Froody> documentation for details of who has worked
183 on this project.
184  
185 This module is free software; you can redistribute it and/or modify it under
186 the same terms as Perl itself.
187  
188 =head1 SEE ALSO
189  
190 L<Froody>, L<Froody::Response>
191  
192 =cut
193  
194 33 1;
« blib/lib/Froody/Base.pm ↑ Summary blib/lib/Froody/Plugin.pm »