Sha256: 5ee60625f0dd8e1d63ca42246a0c40d56c80f4a8fbb92fec441dfecf80000630

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  # 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`.
  class ApiErrorHandler
    include Dry::Monads::Result::Mixin

    # @return [Class] the class used to wrap the API error
    attr_reader :api_error_class

    # @param api_error_class [Class] the class used to wrap the API error
    def initialize(api_error_class: FriendlyShipping::ApiError)
      @api_error_class = api_error_class
    end

    # @param error [StandardError] the error to handle
    # @param original_request [Request] the API request which triggered the error
    # @param original_response [RestClient::Response] the API response containing the error
    # @return [Failure<ApiResult>]
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
friendly_shipping-0.10.1 lib/friendly_shipping/api_error_handler.rb
friendly_shipping-0.10.0 lib/friendly_shipping/api_error_handler.rb