class CloudflareClient::Middleware::Response::RaiseError

Raises ResponseError exceptions when a response status in either the 4xx range or the 5xx range is encountered. There are a number of specific exception mappings as well as general exception types covering the respective ranges.

Constants

CLIENT_ERRORS
SERVER_ERRORS

Public Class Methods

new(app) click to toggle source
# File lib/cloudflare_client/middleware/response/raise_error.rb, line 53
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/cloudflare_client/middleware/response/raise_error.rb, line 57
def call(env)
  response = @app.call(env)
  handle_status(response, env.method, env.url.request_uri, env.url.to_s)
  response
end

Private Instance Methods

handle_status(response, method, uri, url) click to toggle source
# File lib/cloudflare_client/middleware/response/raise_error.rb, line 65
def handle_status(response, method, uri, url)
  case response.status
  when 400
    raise CloudflareClient::BadRequest.new('400 Bad Request', response, method, uri, url)
  when 401
    raise CloudflareClient::Unauthorized.new('401 Unauthorized', response, method, uri, url)
  when 403
    raise CloudflareClient::Forbidden.new('403 Forbidden', response, method, uri, url)
  when 404
    raise CloudflareClient::ResourceNotFound.new('404 Not Found', response, method, uri, url)
  when 407
    # Mimic the behavior that we get with proxy requests with HTTPS. We still
    # use Faraday exceptions for network errors, as it's difficult to reliably
    # wrap these in CloudflareClient without losing information.
    raise Faraday::ConnectionFailed.new('407 Proxy Authentication Required', response)
  when 409
    raise CloudflareClient::Conflict.new('409 Conflict', response, method, uri, url)
  when 410
    raise CloudflareClient::Gone.new('410 Gone', response, method, uri, url)
  when 412
    raise CloudflareClient::PreconditionFailed.new('412 Precondition Failed', response, method, uri, url)
  when 422
    raise CloudflareClient::UnprocessableEntity.new('422 Unprocessable Entity', response, method, uri, url)
  when 423
    raise CloudflareClient::Locked.new('423 Locked', response, method, uri, url)
  when 429
    raise CloudflareClient::TooManyRequests.new('429 Too Many Requests', response, method, uri, url)
  when 500
    raise CloudflareClient::InternalServerError.new('500 Internal Server Error', response, method, uri, url)
  when 502
    raise CloudflareClient::BadGateway.new('502 Bad Gateway', response, method, uri, url)
  when 503
    raise CloudflareClient::ServiceUnavailable.new('503 Service Unavailable', response, method, uri, url)
  when 504
    raise CloudflareClient::GatewayTimeout.new('504 Gateway Timeout', response, method, uri, url)
  when CLIENT_ERRORS
    raise CloudflareClient::ClientError.new(response.status.to_s, response, method, uri, url)
  when SERVER_ERRORS
    raise CloudflareClient::ServerError.new(response.status.to_s, response, method, uri, url)
  end
end