File Coverage

blib/lib/OIDC/Client/ResponseParser.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition 5 8 62.5
subroutine 10 10 100.0
pod 1 1 100.0
total 45 48 93.7


line stmt bran cond sub pod time code
1             package OIDC::Client::ResponseParser;
2 7     7   543811 use utf8;
  7         264  
  7         65  
3 7     7   819 use Moose;
  7         549024  
  7         123  
4 7     7   62423 use namespace::autoclean;
  7         10893  
  7         97  
5              
6 7     7   649 use Try::Tiny;
  7         15  
  7         576  
7 7     7   4264 use OIDC::Client::Error::InvalidResponse;
  7         125  
  7         355  
8 7     7   4493 use OIDC::Client::Error::Provider;
  7         101  
  7         2057  
9              
10             =encoding utf8
11              
12             =head1 NAME
13              
14             OIDC::Client::ResponseParser - Provider response parser
15              
16             =head1 DESCRIPTION
17              
18             Provider response parser.
19              
20             =head1 METHODS
21              
22             =head2 parse( $response )
23              
24             Analyse the result of the L<Mojo::Message::Response> object.
25              
26             If all is well, returns the Perl structure corresponding to the JSON response.
27              
28             On the other hand, if the response is not correctly parsed into JSON format,
29             an L<OIDC::Client::Error::InvalidResponse> error is thrown or if the provider
30             returned an error, an L<OIDC::Client::Error::Provider> error is thrown.
31              
32             =cut
33              
34             sub parse {
35 7     7 1 223 my ($self, $res) = @_;
36              
37 7 100       52 if ($res->is_success) {
38             return try {
39 4     4   338 $res->json;
40             }
41             catch {
42 2     2   138 OIDC::Client::Error::InvalidResponse->throw(
43             sprintf(q{Invalid response: %s}, $_)
44             );
45 4         261 };
46             }
47             else {
48             OIDC::Client::Error::Provider->throw({
49 3     3   193 response_parameters => try { $res->json } || {},
50             alternative_error => $res->is_error ? $res->message || $res->code
51             : $res->{error}{message} || $res->{error}{code},
52 3 100 100     191 });
      66        
      33        
53             }
54             }
55              
56             __PACKAGE__->meta->make_immutable;
57              
58             1;