class Namira::Errors::HTTPError

HTTP Error

Any non 2xx status code will raise this error.

Constants

STATUS_MAPPING

Attributes

response[R]

@return [Namira::Response] The HTTP response

status[R]

@return [Integer] The HTTP status that caused the error

Public Class Methods

create(response) click to toggle source

Returns a new HTTP Error based on the status

# File lib/namira/errors/http_error.rb, line 31
def create(response)
  klass_for_status(response.status).new("http_error/#{response.status}", response.status, response)
end
generate_custom_classes() click to toggle source
# File lib/namira/errors/http_error.rb, line 35
def generate_custom_classes
  STATUS_MAPPING.each do |_, value|
    klass_name = "#{value.tr(' ', '')}Error"
    begin
      HTTPError.const_get(klass_name)
    rescue NameError
      klass = Class.new(HTTPError) {}
      HTTPError.const_set(klass_name, klass)
    end
  end
end
new(msg, status, response) click to toggle source

Returns a new instance of HTTPError

@param msg [String] The error message. e.g. “http_error/500” @param status [Integer] The HTTP status that caused the error @param response [Namira::Response] The HTTP response

Calls superclass method
# File lib/namira/errors/http_error.rb, line 22
def initialize(msg, status, response)
  @status = status
  @response = response
  super(msg)
end

Private Class Methods

klass_for_status(status) click to toggle source
# File lib/namira/errors/http_error.rb, line 49
def klass_for_status(status)
  name = STATUS_MAPPING[status.to_i.to_s]
  return HTTPError if name.nil?

  klass_name = "#{name.tr(' ', '')}Error"
  begin
    HTTPError.const_get(klass_name)
  rescue NameError
    return HTTPError
  end
end