Sha256: b4158da6105b566e2320b8a9603a0f90f59bc11141d24555f1b9428587703d46

Contents?: true

Size: 1022 Bytes

Versions: 4

Compression:

Stored size: 1022 Bytes

Contents

# frozen_string_literal: true

module TopsConnect
  class ApiError < ::RuntimeError
    def initialize(response)
      @response = response
    end

    def to_s
      format(
        '%<code>s: %<message>s (%<uri>s)',
        code: @response.code,
        message: self.class.error_message(@response.parsed_response),
        uri: @response.request.last_uri.to_s
      )
    end

    def self.error_message(parsed_response)
      case parsed_response
      when String
        parsed_response
      when Hash
        parsed_response.dig('Message')
      else
        ''
      end
    end
  end

  # The client submitted invalid information.
  class ClientError < ApiError
  end

  # A request was made for a key/query that doesn't exist.
  class NotFoundError < ClientError
  end

  # Something happened but we don't know what and it's probably not our fault.
  class InternalError < ApiError
  end

  # The API took too long to respond, but everything might be fine later.
  class TimeoutError < InternalError
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tops_connect-0.6.3 lib/tops_connect/errors.rb
tops_connect-0.6.2 lib/tops_connect/errors.rb
tops_connect-0.6.1 lib/tops_connect/errors.rb
tops_connect-0.6.0 lib/tops_connect/errors.rb