Sha256: e39a5151781e3d4a8916f2b07adc9459dfb02b6c1e9f56edae49189358876742

Contents?: true

Size: 820 Bytes

Versions: 4

Compression:

Stored size: 820 Bytes

Contents

module Zesty
  class Error < StandardError

    attr_reader :code, :response_body, :response_headers

    class << self
      def from_response(data, response)
        message = parse_error(data) || response.to_s
        new(message, response.code, response.to_s, response.headers.to_h)
      end

      private

      def parse_error(data)
        if data.is_a?(Hash)
          data.dig(:message) || data.dig(:error)
        end
      end
    end

    def initialize(message, code, response_body, response_headers)
      super(message)
      @code = code
      @response_body = response_body
      @response_headers = response_headers
    end

    def to_h
      {
        code: code,
        message: message,
        response_body: response_body,
        response_headers: response_headers
      }
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zesty-0.3.0 lib/zesty/error.rb
zesty-0.2.1 lib/zesty/error.rb
zesty-0.2.0 lib/zesty/error.rb
zesty-0.1.0 lib/zesty/error.rb