File Coverage

blib/lib/Dancer/RPCPlugin/ErrorResponse.pm
Criterion Covered Total %
statement 27 27 100.0
branch 5 6 83.3
condition n/a
subroutine 12 12 100.0
pod 8 8 100.0
total 52 53 98.1


line stmt bran cond sub pod time code
1             package Dancer::RPCPlugin::ErrorResponse;
2 8     8   122788 use warnings;
  8         32  
  8         270  
3 8     8   47 use strict;
  8         14  
  8         181  
4              
5 8     8   39 use Exporter 'import';
  8         18  
  8         396  
6             our @EXPORT = qw/error_response/;
7              
8 8     8   432 use Params::ValidationCompiler 'validation_for';
  8         18204  
  8         2227  
9              
10             sub error_response {
11 4     4 1 673 __PACKAGE__->new(@_);
12             }
13              
14             sub new {
15 9     9 1 19 my $class = shift;
16 9         70 my %self = validation_for(
17             params => {
18             error_code => {optional => 0},
19             error_message => {optional => 0},
20             error_data => {optional => 1},
21             },
22             )->(@_);
23              
24 9         6202 return bless(\%self, $class);
25             }
26              
27 13     13 1 776 sub error_code { $_[0]->{error_code} }
28 11     11 1 74 sub error_message { $_[0]->{error_message} }
29 11 100   11 1 41 sub error_data { exists $_[0]->{error_data} ? $_[0]->{error_data} : undef }
30              
31             sub as_xmlrpc_fault {
32 2     2 1 4 my $self = shift;
33             return {
34 2         5 faultCode => $self->error_code,
35             faultString => $self->error_message,
36             };
37             }
38              
39             sub as_jsonrpc_error {
40 3     3 1 7 my $self = shift;
41              
42 3         9 my $data = $self->error_data;
43             return {
44 3 100       10 error => {
45             code => $self->error_code,
46             message => $self->error_message,
47             ($data ? (data => $data) : ()),
48             }
49             };
50             }
51              
52             sub as_restrpc_error {
53 6     6 1 16 my $self = shift;
54              
55 6         20 my $data = $self->error_data;
56             return {
57 6 50       20 error => {
58             code => $self->error_code,
59             message => $self->error_message,
60             ($data ? (data => $data) : ()),
61             }
62             };
63             }
64              
65              
66             1;
67              
68             =head1 NAME
69              
70             Dancer::RPCPlugin::ErrorResponse - Interface to pass error-responses without knowlage of the protocol
71              
72             =head1 SYNOPSIS
73              
74             use Dancer::RPCPlugin::ErrorResponse;
75              
76             sub handle_rpc_call {
77             ...
78             return error_response(
79             error_code => 42,
80             error_message => 'That went belly-up',
81             );
82             }
83              
84             =head1 DESCRIPTION
85              
86             =head2 error_response(%parameters)
87              
88             Factory function that retuns an instantiated L<Dancer::RPCPlugin::ErrorResponse>.
89              
90             =head3 Parameters
91              
92             =over
93              
94             =item error_code => $error_code [required]
95              
96             =item error_message => $error_message [required]
97              
98             =item error_data => $error_data [optional]
99              
100             =back
101              
102             =head3 Responses
103              
104             An instance or an exception from L<Params::ValidationCompiler>.
105              
106             =head2 Dancer::RPCPlugin::ErrorResponse->new(%parameters)
107              
108             =head3 Parameters
109              
110             =over
111              
112             =item error_code => $error_code [required]
113              
114             =item error_message => $error_message [required]
115              
116             =item error_data => $error_data [optional]
117              
118             =back
119              
120             =head3 Responses
121              
122             An instance or an exception from L<Params::ValidationCompiler>.
123              
124             =head2 $er->error_code
125              
126             Getter for the C<error_code> attribute.
127              
128             =head2 $er->error_message
129              
130             Getter for the C<error_message> attribute.
131              
132             =head2 $er->error_data
133              
134             Getter for the C<error_data> attribute.
135              
136             =head2 $er->as_jsonrpc_error
137              
138             Returns a data-structure for the use in the C<error> field of a jsonrpc response.
139              
140             =head2 $er->as_xmlrpc_fault
141              
142             Returns a data-structure for the use as a C<fault> response in XMLRPC.
143              
144             =head2 $er->as_restrpc_error
145              
146             Returns a data-structure like the C<error-field> in a JSONRPC2 error response.
147              
148             =head1 COPYRIGHT
149              
150             (c) MMXVII - Abe Timmerman <abetim@cpan.org>
151              
152             =cut