File Coverage

blib/lib/Kubernetes/REST/Result2Hash.pm
Criterion Covered Total %
statement 17 24 70.8
branch 2 8 25.0
condition n/a
subroutine 5 6 83.3
pod 0 3 0.0
total 24 41 58.5


line stmt bran cond sub pod time code
1             package Kubernetes::REST::Result2Hash;
2 1     1   56207 use Moo;
  1         8882  
  1         4  
3 1     1   1495 use JSON::MaybeXS;
  1         6063  
  1         59  
4 1     1   316 use Kubernetes::REST::Error;
  1         3  
  1         287  
5              
6             has parser => (is => 'ro', default => sub { JSON::MaybeXS->new });
7              
8             sub result2return {
9 1     1 0 1801 my ($self, $call, $req, $response) = @_;
10              
11 1 50       6 if ($response->status >= 400) {
12 1         4 return $self->process_error($response);
13             } else {
14 0 0       0 return 1 if (not defined $response->content);
15 0         0 return $self->process_response($response);
16             }
17             }
18              
19             sub process_response {
20 0     0 0 0 my ($self, $response) = @_;
21            
22 0         0 my $struct = eval {
23 0         0 $self->parser->decode($response->content);
24             };
25 0 0       0 Kubernetes::REST::Error->throw(
26             type => 'UnparseableResponse',
27             message => 'Can\'t parse response ' . $response->content . ' with error ' . $@
28             ) if ($@);
29              
30 0         0 return $struct;
31             }
32              
33             sub process_error {
34 1     1 0 2 my ($self, $response) = @_;
35              
36 1         2 my $struct = eval {
37 1         12 $self->parser->decode($response->content);
38             };
39              
40 1 50       3 Kubernetes::REST::Error->throw(
41             type => 'UnparseableResponse',
42             message => 'Can\'t parse JSON content',
43             detail => $response->content,
44             ) if ($@);
45              
46             # Throw a Kubernetes::REST::RemoteError exception from
47             # the info in $struct
48             # {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401}
49 1         15 Kubernetes::REST::RemoteError->throw(
50             status => $response->status,
51             type => 'RemoteError',
52             message => "$struct->{ message }: $struct->{ reason }",
53             );
54             }
55              
56             1;