| 1 | |||
| 2 | package Net::Simplify::ApiException; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Net::Simplify::ApiException - Simplify Commerce exception base class. | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use Net::Simplify; | ||
| 11 | |||
| 12 | $Net::Simplify::public_key = 'YOUR PUBLIC KEY'; | ||
| 13 | $Net::Simplify::private_key = 'YOUR PRIVATE KEY'; | ||
| 14 | |||
| 15 | eval { | ||
| 16 | my $payment = Net::Simplify::Payment->create(...); | ||
| 17 | }; | ||
| 18 | if ($@) { | ||
| 19 | if ($@->isa('Net::Simplify::ApiException')) { | ||
| 20 | my $message = $@->message; | ||
| 21 | my $code = $@->code; | ||
| 22 | printf "API Exception: %s %s\n", $@->message, $@->code; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | All exceptions thrown when using the API have the ApiException as their base class. | ||
| 29 | |||
| 30 | =head3 status() | ||
| 31 | |||
| 32 | Returns the HTTP status code for the API exception (if any). | ||
| 33 | |||
| 34 | =head3 code() | ||
| 35 | |||
| 36 | Returns error code for the API exeption (if any). | ||
| 37 | |||
| 38 | =head3 reference() | ||
| 39 | |||
| 40 | Returns the reference string for the API exception (if any). | ||
| 41 | |||
| 42 | =head3 message() | ||
| 43 | |||
| 44 | Returns the error message for the API exception. | ||
| 45 | |||
| 46 | =head3 stringify() | ||
| 47 | |||
| 48 | Returns a string representation of the exception. | ||
| 49 | |||
| 50 | =head3 longmess() | ||
| 51 | |||
| 52 | Returns the callstack. | ||
| 53 | |||
| 54 | =head1 SEE ALSO | ||
| 55 | |||
| 56 | L<Net::Simplify>, | ||
| 57 | L<Net::Simplify::IllegalArgumentException>, | ||
| 58 | L<Net::Simplify::AuthorizationException>, | ||
| 59 | L<Net::Simplify::ObjectNotFoundException>, | ||
| 60 | L<Net::Simplify::NotAllowedException>, | ||
| 61 | L<Net::Simplify::BadRequestException>, | ||
| 62 | L<Net::Simplify::SystemException>, | ||
| 63 | L<Net::Simplify::FieldError>, | ||
| 64 | L<http://www.simplify.com> | ||
| 65 | |||
| 66 | =head1 VERSION | ||
| 67 | |||
| 68 | 1.6.0 | ||
| 69 | |||
| 70 | =head1 LICENSE | ||
| 71 | |||
| 72 | Copyright (c) 2013 - 2022 MasterCard International Incorporated | ||
| 73 | All rights reserved. | ||
| 74 | |||
| 75 | Redistribution and use in source and binary forms, with or without modification, are | ||
| 76 | permitted provided that the following conditions are met: | ||
| 77 | |||
| 78 | Redistributions of source code must retain the above copyright notice, this list of | ||
| 79 | conditions and the following disclaimer. | ||
| 80 | Redistributions in binary form must reproduce the above copyright notice, this list of | ||
| 81 | conditions and the following disclaimer in the documentation and/or other materials | ||
| 82 | provided with the distribution. | ||
| 83 | Neither the name of the MasterCard International Incorporated nor the names of its | ||
| 84 | contributors may be used to endorse or promote products derived from this software | ||
| 85 | without specific prior written permission. | ||
| 86 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
| 87 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 88 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| 89 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 90 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
| 91 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| 92 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
| 93 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 94 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 95 | SUCH DAMAGE. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | use 5.006; | ||
| 100 | use strict; | ||
| 101 | use warnings FATAL => 'all'; | ||
| 102 | |||
| 103 | use Carp; | ||
| 104 | use overload ('""' => 'stringify'); | ||
| 105 | |||
| 106 | sub new { | ||
| 107 | my ($class, $message, $status, $error_data) = @_; | ||
| 108 | |||
| 109 | my $self = { | ||
| 110 | message => $message, | ||
| 111 | status => $status, | ||
| 112 | longmess => Carp::longmess, | ||
| 113 | errorData => $error_data, | ||
| 114 | field_errors => [] | ||
| 115 | }; | ||
| 116 | |||
| 117 | if ($error_data) { | ||
| 118 | $self->{reference} = $error_data->{reference}; | ||
| 119 | my $error = $error_data->{error}; | ||
| 120 | if (defined $error) { | ||
| 121 | $self->{code} = $error->{code}; | ||
| 122 | } | ||
| 123 | $self->{message} = $error->{message} if defined $error->{message}; | ||
| 124 | } | ||
| 125 | bless $self, $class; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub status { | ||
| 129 | my ($self) = @_; | ||
| 130 | $self->{status}; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub code { | ||
| 134 | my ($self) = @_; | ||
| 135 | $self->{code}; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub reference { | ||
| 139 | my ($self) = @_; | ||
| 140 | $self->{reference}; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub message { | ||
| 144 | my ($self) = @_; | ||
| 145 | $self->{message}; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub longmess { | ||
| 149 | my ($self) = @_; | ||
| 150 | $self->{longmess}; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub stringify { | ||
| 154 | my ($self) = @_; | ||
| 155 | |||
| 156 | my $class = ref $self; | ||
| 157 | my $status = defined $self->{status} ? $self->{status} : 'n/a'; | ||
| 158 | my $code = defined $self->{code} ? $self->{code} : 'n/a'; | ||
| 159 | my $reference = defined $self->{reference} ? $self->{reference} : 'n/a'; | ||
| 160 | my $message = $self->{message}; | ||
| 161 | |||
| 162 | "${class}: '${message}' (status: ${status}, code: ${code}, reference ${reference})\n"; | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; |