File Coverage

blib/lib/Business/GoCardless/Exception.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Business::GoCardless::Exception;
2              
3             =head1 NAME
4              
5             Business::GoCardless::Exception
6              
7             =head1 DESCRIPTION
8              
9             Exception handling for the Business::GoCardless modules, uses the Throwable
10             module.
11              
12             =cut
13              
14 20     20   750 use strict;
  20         42  
  20         506  
15 20     20   95 use warnings;
  20         36  
  20         431  
16              
17 20     20   492 use Moo;
  20         8588  
  20         87  
18 20     20   14013 use JSON ();
  20         115532  
  20         551  
19 20     20   128 use Carp qw/ cluck /;
  20         36  
  20         6528  
20              
21             with 'Throwable';
22              
23             =head1 ATTRIBUTES
24              
25             =head2 message
26              
27             The error message, if JSON is passed this will be coerced to a string.
28              
29             =head2 code
30              
31             The error code, generally the HTTP status code.
32              
33             =head2 response
34              
35             The error response, generally the HTTP response.
36              
37             =cut
38              
39             # plain string or JSON
40             has message => (
41             is => 'ro',
42             required => 1,
43             coerce => sub {
44             my ( $message ) = @_;
45              
46             cluck $message if $ENV{GOCARDLESS_DEV_TESTING};
47              
48             if ( $message =~ /^[{\[]/ ) {
49             # defensive decoding
50             eval { $message = JSON->new->decode( $message ) };
51             $@ && do { return "Failed to parse JSON response ($message): $@"; };
52              
53             if ( ref( $message ) eq 'HASH' ) {
54             my $error = delete( $message->{error} ) // "Unknown error";
55              
56             if ( ref( $error ) eq 'HASH' ) {
57              
58             my $mess = $error->{message};
59              
60             foreach my $sub_error ( @{ $error->{errors} // [] } ) {
61             $mess .= ' / ' . $sub_error->{message};
62             }
63              
64             return $mess . " << $mess >> ";
65              
66             } elsif ( ref( $error ) eq 'ARRAY' ) {
67             return join( ', ',@{ $error } );
68             } else {
69             return $error;
70             }
71              
72             } else {
73             return join( ', ',@{ $message } );
74             }
75             } else {
76             return $message;
77             }
78             },
79             );
80              
81             # generally the HTTP status code
82             has code => (
83             is => 'ro',
84             required => 0,
85             );
86              
87             # generally the HTTP status code + message
88             has response => (
89             is => 'ro',
90             required => 0,
91             );
92              
93             =head1 METHODS
94              
95             =head2 description
96              
97             An alias to the message attribute.
98              
99             =cut
100              
101             # compatibility with ruby lib
102 1     1 1 942 sub description { shift->message }
103              
104             =head1 AUTHOR
105              
106             Lee Johnson - C
107              
108             This library is free software; you can redistribute it and/or modify it under
109             the same terms as Perl itself. If you would like to contribute documentation,
110             features, bug fixes, or anything else then please raise an issue / pull request:
111              
112             https://github.com/Humanstate/business-gocardless
113              
114             =cut
115              
116             1;
117              
118             # vim: ts=4:sw=4:et