Exception: Namira::Errors::HTTPError

Inherits:
BaseError
  • Object
show all
Defined in:
lib/namira/errors/http_error.rb

Overview

HTTP Error

Any non 2xx status code will raise this error.

Constant Summary collapse

STATUS_MAPPING =
{
  '300' => 'Multiple Choices',
  '301' => 'Moved Permanently',
  '302' => 'Found',
  '303' => 'See Other',
  '304' => 'Not Modified',
  '305' => 'Use Proxy',
  '306' => 'Switch Proxy',
  '307' => 'Temporary Redirect',
  '308' => 'Permanent Redirect',
  '400' => 'Bad Request',
  '401' => 'Unauthorized',
  '402' => 'Payment Required',
  '403' => 'Foridden',
  '404' => 'Not Found',
  '405' => 'Method Not Allowed',
  '406' => 'Not Acceptable',
  '407' => 'Proxy Authentication Required',
  '408' => 'Request Timeout',
  '409' => 'Conflict',
  '410' => 'Gone',
  '411' => 'Length Required',
  '412' => 'Precondition Failed',
  '413' => 'Payload Too Large',
  '414' => 'URI Too Long',
  '415' => 'Unsupported Media Type',
  '416' => 'Range Not Satisfiable',
  '417' => 'Expectation Failed',
  '418' => 'Im A Teapot',
  '421' => 'Misdirected Request',
  '422' => 'Unprocessable Entity',
  '423' => 'Locked',
  '424' => 'Failed Dependency',
  '426' => 'Upgrade Required',
  '428' => 'Precondition Required',
  '429' => 'Too Man Requests',
  '431' => 'Request Header Fields Too Large',
  '451' => 'Unavailable For Legal Reasons',
  '500' => 'Internal Server Error',
  '501' => 'Not Implemented',
  '502' => 'Bad Gateway',
  '503' => 'Service Unavailable',
  '504' => 'Gateway Timeout',
  '505' => 'HTTP Version Not Supported',
  '506' => 'Variant Also Negotiates',
  '507' => 'Insufficient Storage',
  '508' => 'Loop Detected',
  '510' => 'Not Extended',
  '511' => 'Network Authentication Required'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, status, response) ⇒ HTTPError

Returns a new instance of HTTPError

Parameters:

  • msg (String)

    The error message. e.g. “http_error/500”

  • status (Integer)

    The HTTP status that caused the error

  • response (Namira::Response)

    The HTTP response



22
23
24
25
26
# File 'lib/namira/errors/http_error.rb', line 22

def initialize(msg, status, response)
  @status = status
  @response = response
  super(msg)
end

Instance Attribute Details

#responseNamira::Response (readonly)

Returns The HTTP response.

Returns:



14
15
16
# File 'lib/namira/errors/http_error.rb', line 14

def response
  @response
end

#statusInteger (readonly)

Returns The HTTP status that caused the error.

Returns:

  • (Integer)

    The HTTP status that caused the error



10
11
12
# File 'lib/namira/errors/http_error.rb', line 10

def status
  @status
end

Class Method Details

.create(response) ⇒ Object

Returns a new HTTP Error based on the status



31
32
33
# 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_classesObject



35
36
37
38
39
40
41
42
43
44
45
# 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