Module: Veeqo::HttpErrors

Included in:
Middleware::HttpException
Defined in:
lib/veeqo/exception.rb

Constant Summary collapse

ERRORS =
{
  400 => Veeqo::BadRequest,
  401 => Veeqo::Unauthorized,
  403 => Veeqo::Forbidden,
  404 => Veeqo::NotFound,
  405 => Veeqo::MethodNotAllowed,
  406 => Veeqo::NotAccepted,
  408 => Veeqo::TimeOut,
  409 => Veeqo::ResourceConflict,
  429 => Veeqo::TooManyRequests,
  500 => Veeqo::InternalServerError,
  502 => Veeqo::BadGateway,
  503 => Veeqo::ServiceUnavailable,
  504 => Veeqo::GatewayTimeout,
  509 => Veeqo::BandwidthLimitExceeded
}.freeze

Instance Method Summary collapse

Instance Method Details

#throw_http_exception!(code, env) ⇒ Object

Raises:

  • (ERRORS[code].new(response_headers))


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/veeqo/exception.rb', line 42

def throw_http_exception!(code, env)
  return unless ERRORS.keys.include? code
  response_headers = {}
  unless env.body.empty?
    response_headers = begin
      Oj.load(env.body, symbol_keys: true)
    rescue
      {}
    end
  end
  unless env[:response_headers] && env[:response_headers]['X-Retry-After'].nil?
    response_headers[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
  end
  raise ERRORS[code].new(response_headers), env.body
end