Sha256: c2c291aa4753c0dd83640ec5a237f80fc7cb03bd686300504b72f3f52b6e4f97

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 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} (code: #{code}, body: #{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 InvalidResponseSignature < ResponseError; end
  class AbsentResponseSignature < ResponseError; end
  class TooManyRequestsResponse < ResponseError; end
  class UnauthorisedResponse < ResponseError; end
  class ResourceNotFound < ResponseError; end
  class Timeout < StandardError; end
  class MaintenanceResponse < ResponseError; end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bunq-client-0.7.2 lib/bunq/errors.rb
bunq-client-0.7.1 lib/bunq/errors.rb
bunq-client-0.7.0 lib/bunq/errors.rb