Sha256: d0f5d37f428122cf2918f098004a5c8d4c139dcfa8486dce0c6eb0ac4e5dbf4e

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Bunq
  class ResponseError < StandardError
    attr_reader :code
    attr_reader :headers
    attr_reader :body

    def initialize(msg = "Response error", code: nil, headers: nil, body: nil)
      @code = code
      @headers = headers || {}
      @body = body
      super("#{msg}: #{body}")
    end

    # Returns the parsed body if it is a JSON document, nil otherwise.
    # @param opts [Hash] Optional options that are passed to `JSON.parse`.
    def parsed_body(opts = {})
      JSON.parse(@body, opts) if @body && @headers['content-type'] && @headers['content-type'].include?('application/json')
    end

    # Returns an array of errors returned from the API, or nil if no errors are returned.
    # @return [Array|nil]
    def errors
      json = parsed_body
      json ? json['Error'] : nil
    end
  end

  class UnexpectedResponse < ResponseError; end
  class AbsentResponseSignature < ResponseError; end
  class TooManyRequestsResponse < ResponseError; end
  class UnauthorisedResponse < ResponseError; end
  class Timeout < StandardError; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunq-client-0.2.0 lib/bunq/errors.rb