class Essential::APIError

Attributes

body[R]
code[R]
http_status[R]
message[R]
params[R]
response[R]
type[R]

Public Class Methods

from_exception(e) click to toggle source
# File lib/essential/errors/api_error.rb, line 8
def self.from_exception(e)
  case e
  # when SocketError
  # when NoMethodError
  when RestClient::ExceptionWithResponse
    new(
      http_status: e.http_code,
      body: e.http_body,
      response: e.response
    )
  when RestClient::Exception
    new(http_status: e.http_code)
  # when Errno::ECONNREFUSED
  else
    raise e
  end
end
new(http_status: nil, body: nil, response: nil) click to toggle source
# File lib/essential/errors/api_error.rb, line 26
def initialize(http_status: nil, body: nil, response: nil)
  @http_status = http_status
  @response = response

  if body
    begin
      @body = JSON.parse(body)
      if @body.key?('error')
        error    = @body['error']
        @type    = error['type']
        @message = error['message']
        @code    = error['code']
        @params  = error['params']
      end
    rescue JSON::ParserError
      @body = body
    end
  end
end

Public Instance Methods

to_s() click to toggle source
# File lib/essential/errors/api_error.rb, line 46
def to_s
  parts = []
  parts << format('<%s>', self.http_status)
  parts << self.type
  parts << self.message if self.message
  parts << self.params.to_json if self.params
  parts.join(' - ')
end