class Api::Error

All errors from this gem will inherit from this one.

Public Class Methods

from_response(response) click to toggle source

Returns the appropriate Api::Error subclass based on status and response message

@param [Hash] response HTTP response @return [Api::Error]

# File lib/api/errors.rb, line 10
def self.from_response(response)
  status = if response.respond_to?(:[])
             response[:status].to_i
           else
             response.status
           end

  if klass =  case status
              when 400      then Api::BadRequest
              when 401      then Api::Unauthorized
              when 403      then Api::Unauthorized
              when 404      then Api::NotFound
              when 405      then Api::MethodNotAllowed
              when 406      then Api::NotAcceptable
              when 409      then Api::Conflict
              when 415      then Api::UnsupportedMediaType
              when 422      then Api::UnprocessableEntity
              when 400..499 then Api::ClientError
              when 500      then Api::InternalServerError
              when 501      then Api::NotImplemented
              when 502      then Api::BadGateway
              when 503      then Api::ServiceUnavailable
              when 500..599 then Api::ServerError
              end
    klass.new(response)
  end
end
new(response=nil) click to toggle source
Calls superclass method
# File lib/api/errors.rb, line 38
def initialize(response=nil)
  @response = response
  super
end