File Coverage

lib/Business/Fixflo/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::Fixflo::Exception;
2              
3             =head1 NAME
4              
5             Business::Fixflo::Exception
6              
7             =head1 DESCRIPTION
8              
9             Exception handling for the Business::Fixflo modules, uses the Throwable
10             module.
11              
12             =cut
13              
14 17     17   846 use strict;
  17         40  
  17         545  
15 17     17   93 use warnings;
  17         39  
  17         410  
16              
17 17     17   614 use Moo;
  17         10915  
  17         91  
18 17     17   18648 use JSON ();
  17         181378  
  17         619  
19 17     17   145 use Carp qw/ cluck /;
  17         37  
  17         5415  
20              
21             with 'Throwable';
22              
23             =head1 ATTRIBUTES
24              
25             =head2 message (required)
26              
27             The error message, if JSON is passed this will be coerced to a string.
28              
29             =head2 code (optional)
30              
31             The error code, generally the HTTP status code.
32              
33             =head2 response (optional)
34              
35             The error response, generally the HTTP response.
36              
37             =head2 request (optional)
38              
39             The original HTTP request data including path, params, content, and headers.
40             You should be careful if you write this data to a file as it may contain
41             sensitive information such as API key(s).
42              
43             =cut
44              
45             # plain string or JSON
46             has message => (
47             is => 'ro',
48             required => 1,
49             coerce => sub {
50             my ( $message ) = @_;
51              
52             cluck $message if $ENV{FIXFLO_DEBUG};
53              
54             if ( $message =~ /^[{\[]/ ) {
55             # defensive decoding
56             eval { $message = JSON->new->decode( $message ) };
57             $@ && do { return "Failed to parse JSON response ($message): $@"; };
58              
59             if ( ref( $message ) eq 'HASH' ) {
60             my $error = delete( $message->{Message} ) // "Unknown error";
61             return ref( $error ) ? join( ', ',@{ $error } ) : $error;
62             } else {
63             return join( ', ',@{ $message } );
64             }
65             } else {
66             return $message;
67             }
68             },
69             );
70              
71             # HTTP status code, response, request
72             has [ qw/ code response / ] => (
73             is => 'ro',
74             required => 0,
75             );
76              
77             has [ qw/ request / ] => (
78             is => 'ro',
79             required => 0,
80             default => sub { $Business::Fixflo::Client::request_data || undef },
81             );
82              
83             =head1 METHODS
84              
85             =head2 description
86              
87             An alias to the message attribute.
88              
89             =cut
90              
91             # compatibility with ruby lib
92 1     1 1 1036 sub description { shift->message }
93              
94             =head1 AUTHOR
95              
96             Lee Johnson - C
97              
98             This library is free software; you can redistribute it and/or modify it under
99             the same terms as Perl itself. If you would like to contribute documentation,
100             features, bug fixes, or anything else then please raise an issue / pull request:
101              
102             https://github.com/Humanstate/business-fixflo
103              
104             =cut
105              
106             1;
107              
108             # vim: ts=4:sw=4:et