Exception: PaystackGateway::ApiError

Inherits:
StandardError
  • Object
show all
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
    

Constant Summary collapse

CONNECTION_ERROR_CLASSES =
[
  Faraday::ServerError,
  Faraday::ConnectionFailed,
  Faraday::SSLError,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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?')
    matching_messages = [
      method_name.to_s.remove(/_error\?$/),
      method_name.to_s.remove(/\?$/),
    ]
    message.to_s.in?(matching_messages)
  end
end

Instance Attribute Details

#cancellableObject (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_errorObject (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

Returns:

  • (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

Returns:

  • (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