class Bitsor::Error

Attributes

body[RW]
request[RW]
response[RW]

Public Class Methods

from_response(response) click to toggle source
# File lib/bitsor/error.rb, line 5
def self.from_response(response)
  status = response.response_code

  if klass =  case status
              when 400      then Bitsor::BadRequest
              when 401      then Bitsor::Forbidden
              when 403      then Bitsor::Forbidden
              when 404      then Bitsor::NotFound
              when 405      then Bitsor::MethodNotAllowed
              when 406      then Bitsor::NotAcceptable
              when 422      then Bitsor::UnprocessableEntity
              when 400..499 then Bitsor::ClientError
              when 500      then Bitsor::InternalServerError
              when 501      then Bitsor::NotImplemented
              when 502      then Bitsor::BadGateway
              when 503      then Bitsor::ServiceUnavailable
              when 500..599 then Bitsor::ServerError
              end
    klass.new(response)
  end
end
new(response = nil) click to toggle source
Calls superclass method
# File lib/bitsor/error.rb, line 27
def initialize(response = nil)
  @response = response
  @request = response.request
  @body = { error: {} }

  begin
    if response.body && !response.body.empty?
      @body = JSON.parse(response.body, symbolize_names: true)
    end
  rescue JSON::ParserError => e
    @body = { error: { code: response.response_code, message: 'Internal Server Error: An Error Was Encountered' } }
  end

  super(build_error_message)
end

Public Instance Methods

build_error_message() click to toggle source
# File lib/bitsor/error.rb, line 43
def build_error_message
  return nil if @response.nil?
  message =  ["#{@request.options[:method].to_s.upcase} "]
  message << @response.options[:effective_url].to_s + "\n"
  message << "Code #{@body[:error][:code]}: #{@body[:error][:message]} \n"
  message.join
end