Data/Riak/HTTP/ExceptionHandler.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 60 CC 9 SCAR 26.0
CC
9
Cov
0%
try_build_exception
20.0
try_build_request_specific_exception
20.0
try_handle_exception
6.0
CRAP 13.4
1 package Data::Riak::HTTP::ExceptionHandler;
2 {
3 $Data::Riak::HTTP::ExceptionHandler::VERSION = '2.0';
4 }
5  
6 use Moose;
7 use namespace::autoclean;
8  
9 has honour_request_specific_exceptions => (
10 is => 'ro',
11 isa => 'Bool',
12 required => 1,
13 builder => '_build_honour_request_specific_exceptions',
14 );
15  
16 has fallback_handlers => (
17 traits => ['Array'],
18 isa => 'ArrayRef[ArrayRef]', # ArrayRef[Tuple[CodeRef,ClassName]]
19 required => 1,
20 builder => '_build_fallback_handler',
21 handles => {
22 fallback_handlers => 'elements',
23 },
24 );
25  
26 sub try_handle_exception {
27 my $e = shift->try_build_exception(@_);
28 die $e if $e;
29 return;
30 }
31  
32 sub try_build_exception {
33 my ($self, $request, $http_request, $http_response) = @_;
34  
35 my ($handled, $expt) = $self->try_build_request_specific_exception(
36 $request, $http_request, $http_response,
37 ) if $self->honour_request_specific_exceptions;
38  
39 return $expt if $expt;
40 return if $handled;
41  
42 return $self->try_build_exception_fallback(
43 $request, $http_request, $http_response,
44 );
45 }
46  
47 sub try_build_request_specific_exception {
48 my ($self, $request, $http_request, $http_response) = @_;
49  
50 return unless $request->does('Data::Riak::Request::WithHTTPExceptionHandling');
51 return unless $request->has_exception_class_for_http_status(
52 $http_response->code,
53 );
54  
55 my $expt_class = $request->exception_class_for_http_status(
56 $http_response->code,
57 );
58  
59 # this status code isn't fatal for this request
60 return (1, undef) if !defined $expt_class;
61  
62 return (0, $expt_class->new({
63 request => $request,
64 transport_request => $http_request,
65 transport_response => $http_response,
66 }));
67 }
68  
69 sub try_build_exception_fallback {
70 my ($self, $request, $http_request, $http_response) = @_;
71  
72 for my $h ($self->fallback_handlers) {
73 my ($matcher, $expt_class) = @{ $h };
74  
75 return $expt_class->new({
76 request => $request,
77 transport_request => $http_request,
78 transport_response => $http_response,
79 }) if $matcher->($http_response->code);
80 }
81 }
82  
83 __PACKAGE__->meta->make_immutable;
84  
85 1;
86  
87 __END__