Exception: PaystackGateway::ApiError
- Inherits:
-
StandardError
- Object
- StandardError
- PaystackGateway::ApiError
- Defined in:
- lib/paystack_gateway/api_error.rb
Overview
This error is raised when an exception occurs in the process of fulfilling an api method.
It allows for dynamic checking of the error message by calling a method.
-
Calling ‘error.foo_error?` will return true if the error message is “foo_error” or “foo”.
-
Calling ‘error.foo_error!` ensures that subsequent calls to `error.foo_error?` return true regardless of the message.
api_method def initialize_transaction(**transaction_data) raise ApiError.new(:invalid_amount, cancellable: true) if !transaction_data[:amount] ... end begin initialize_transaction({amount: nil}) rescue ApiError => e handle_invalid_amount_error(e) if e.invalid_amount_error? # => true cancel_transaction if e.cancellable? end
Direct Known Subclasses
Transactions::InitializeTransactionError, Transactions::VerifyTransactionError, Transfers::InitiateTransferError, Transfers::VerifyTransferError, Verification::ResolveAccountNumberError
Constant Summary collapse
- CONNECTION_ERROR_CLASSES =
[ Faraday::ServerError, Faraday::ConnectionFailed, Faraday::SSLError, ].freeze
Instance Attribute Summary collapse
-
#cancellable ⇒ Object
(also: #cancellable?)
readonly
Returns the value of attribute cancellable.
-
#original_error ⇒ Object
readonly
Returns the value of attribute original_error.
Instance Method Summary collapse
-
#initialize(msg = nil, original_error: nil, cancellable: false) ⇒ ApiError
constructor
A new instance of ApiError.
- #method_missing(method_name) ⇒ Object
- #network_error? ⇒ Boolean
- #respond_to_missing?(method_name) ⇒ Boolean
Constructor Details
#initialize(msg = nil, original_error: nil, cancellable: false) ⇒ ApiError
Returns a new instance of ApiError.
37 38 39 40 41 42 |
# File 'lib/paystack_gateway/api_error.rb', line 37 def initialize(msg = nil, original_error: nil, cancellable: false) super(msg) @original_error = original_error @cancellable = cancellable end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/paystack_gateway/api_error.rb', line 54 def method_missing(method_name, *) return super unless method_name.to_s.end_with?('_error?', '_error!') if method_name.to_s.end_with?('_error!') define_singleton_method(method_name.to_s.sub(/!$/, '?')) { true } elsif method_name.to_s.end_with?('_error?') = [ method_name.to_s.remove(/_error\?$/), method_name.to_s.remove(/\?$/), ] .to_s.in?() end end |
Instance Attribute Details
#cancellable ⇒ Object (readonly) Also known as: cancellable?
Returns the value of attribute cancellable.
34 35 36 |
# File 'lib/paystack_gateway/api_error.rb', line 34 def cancellable @cancellable end |
#original_error ⇒ Object (readonly)
Returns the value of attribute original_error.
34 35 36 |
# File 'lib/paystack_gateway/api_error.rb', line 34 def original_error @original_error end |
Instance Method Details
#network_error? ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'lib/paystack_gateway/api_error.rb', line 44 def network_error? return false if !original_error original_error.class.ancestors.any? do |ancestor| break if ancestor == Exception CONNECTION_ERROR_CLASSES.include?(ancestor) end end |
#respond_to_missing?(method_name) ⇒ Boolean
68 69 70 |
# File 'lib/paystack_gateway/api_error.rb', line 68 def respond_to_missing?(method_name, *) method_name.to_s.end_with?('_error?', '_error!') || super end |