Module: Bigcommerce::HttpErrors

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

Constant Summary collapse

ERRORS =
{
  400 => Bigcommerce::BadRequest,
  401 => Bigcommerce::Unauthorized,
  403 => Bigcommerce::Forbidden,
  404 => Bigcommerce::NotFound,
  405 => Bigcommerce::MethodNotAllowed,
  406 => Bigcommerce::NotAccepted,
  408 => Bigcommerce::TimeOut,
  409 => Bigcommerce::ResourceConflict,
  429 => Bigcommerce::TooManyRequests,
  500 => Bigcommerce::InternalServerError,
  502 => Bigcommerce::BadGateway,
  503 => Bigcommerce::ServiceUnavailable,
  504 => Bigcommerce::GatewayTimeout,
  509 => Bigcommerce::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/bigcommerce/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
      JSON.parse(env.body, symbolize_names: true)
    rescue StandardError
      {}
    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