Class: FriendlyShipping::ApiErrorHandler

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/api_error_handler.rb

Overview

Handles API errors by wrapping them in an API error class (ApiError by default) which is then wrapped in an ApiResult (along with the original API request and response). Finally, the ApiResult is then wrapped in a Dry::Monads::Failure.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_error_class: FriendlyShipping::ApiError) ⇒ ApiErrorHandler

Returns a new instance of ApiErrorHandler.

Parameters:

  • api_error_class (Class) (defaults to: FriendlyShipping::ApiError)

    the class used to wrap the API error



14
15
16
# File 'lib/friendly_shipping/api_error_handler.rb', line 14

def initialize(api_error_class: FriendlyShipping::ApiError)
  @api_error_class = api_error_class
end

Instance Attribute Details

#api_error_classClass (readonly)

Returns the class used to wrap the API error.

Returns:

  • (Class)

    the class used to wrap the API error



11
12
13
# File 'lib/friendly_shipping/api_error_handler.rb', line 11

def api_error_class
  @api_error_class
end

Instance Method Details

#call(error, original_request: nil, original_response: nil) ⇒ Failure<ApiResult>

Parameters:

  • error (StandardError)

    the error to handle

  • original_request (Request) (defaults to: nil)

    the API request which triggered the error

  • original_response (RestClient::Response) (defaults to: nil)

    the API response containing the error

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/friendly_shipping/api_error_handler.rb', line 22

def call(error, original_request: nil, original_response: nil)
  Failure(
    ApiResult.new(
      api_error_class.new(error),
      original_request: original_request,
      original_response: Response.new_from_rest_client_response(original_response)
    )
  )
end