| 1 | |||
| 2 | package Net::Simplify::BadRequestException; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Net::Simplify::BadRequestException - Simplify Commerce exception for bad request errors | ||
| 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::BadRequestException')) { | ||
| 20 | printf "API Exception: %s %s\n", $@->message, $@->code; | ||
| 21 | if ($@->has_field_errors) { | ||
| 22 | foreach my $e ($@->field_errors) { | ||
| 23 | printf "Field error: %s %s %s\n", $e->field, $e->code, $e->message; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | An exception that is thrown for any API call that fails due to errors in the request. If the request contains field errors | ||
| 32 | (for example missing fields or constraint violations) a list of field error objects can be obtained giving details on each | ||
| 33 | error. See the base class L<Net::Simplify::ApiException> for additional of methods. | ||
| 34 | |||
| 35 | =head2 METHODS | ||
| 36 | |||
| 37 | =head3 has_field_errors() | ||
| 38 | |||
| 39 | Whether there are any field errors. | ||
| 40 | |||
| 41 | =head3 field_errors() | ||
| 42 | |||
| 43 | Returns an array of field errors (L<Net::Simplify::FieldError>). | ||
| 44 | |||
| 45 | =head3 stringify() | ||
| 46 | |||
| 47 | Returns a string representation of the exception. | ||
| 48 | |||
| 49 | =head1 SEE ALSO | ||
| 50 | |||
| 51 | L<Net::Simplify>, | ||
| 52 | L<Net::Simplify::ApiException>, | ||
| 53 | L<Net::Simplify::FieldError>, | ||
| 54 | L<http://www.simplify.com> | ||
| 55 | |||
| 56 | =head1 VERSION | ||
| 57 | |||
| 58 | 1.6.0 | ||
| 59 | |||
| 60 | =head1 LICENSE | ||
| 61 | |||
| 62 | Copyright (c) 2013 - 2022 MasterCard International Incorporated | ||
| 63 | All rights reserved. | ||
| 64 | |||
| 65 | Redistribution and use in source and binary forms, with or without modification, are | ||
| 66 | permitted provided that the following conditions are met: | ||
| 67 | |||
| 68 | Redistributions of source code must retain the above copyright notice, this list of | ||
| 69 | conditions and the following disclaimer. | ||
| 70 | Redistributions in binary form must reproduce the above copyright notice, this list of | ||
| 71 | conditions and the following disclaimer in the documentation and/or other materials | ||
| 72 | provided with the distribution. | ||
| 73 | Neither the name of the MasterCard International Incorporated nor the names of its | ||
| 74 | contributors may be used to endorse or promote products derived from this software | ||
| 75 | without specific prior written permission. | ||
| 76 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
| 77 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 78 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| 79 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 80 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
| 81 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| 82 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
| 83 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 84 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 85 | SUCH DAMAGE. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | use 5.006; | ||
| 90 | use strict; | ||
| 91 | use warnings FATAL => 'all'; | ||
| 92 | |||
| 93 | use Net::Simplify::ApiException; | ||
| 94 | |||
| 95 | our @ISA = qw(Net::Simplify::ApiException); | ||
| 96 | |||
| 97 | use overload ('""' => 'stringify'); | ||
| 98 | |||
| 99 | sub new { | ||
| 100 | my ($class, $message, $status, $error_data) = @_; | ||
| 101 | |||
| 102 | my $self = $class->SUPER::new($message, $status, $error_data); | ||
| 103 | |||
| 104 | if ($error_data) { | ||
| 105 | my $error = $error_data->{error}; | ||
| 106 | if (defined $error) { | ||
| 107 | my $field_errors = $error->{fieldErrors}; | ||
| 108 | if (defined $field_errors) { | ||
| 109 | foreach my $field_error (@{$field_errors}) { | ||
| 110 | push(@{$self->{field_errors}}, Net::Simplify::FieldError->new($field_error)); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | bless $self, $class | ||
| 117 | } | ||
| 118 | |||
| 119 | sub has_field_errors { | ||
| 120 | my ($self) = @_; | ||
| 121 | |||
| 122 | my @list = @{$self->{field_errors}}; | ||
| 123 | my $size = @list; | ||
| 124 | |||
| 125 | $size > 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub field_errors { | ||
| 129 | my ($self) = @_; | ||
| 130 | |||
| 131 | @{$self->{field_errors}}; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub stringify { | ||
| 135 | my ($self) = @_; | ||
| 136 | |||
| 137 | my $msg = $self->SUPER::stringify; | ||
| 138 | foreach my $field_error ($self->field_errors()) { | ||
| 139 | $msg .= $field_error; | ||
| 140 | } | ||
| 141 | |||
| 142 | $msg; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | 1; |