Exception: Oauth2ApiClient::ResponseError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/oauth2_api_client/response_error.rb

Overview

The ResponseError class is the main exception class of Oauth2ApiClient and is raised when a request fails with some status code other than 2xx. Using the exception object, you still have access to the response. Moreover, there are exception classes for all 4xx and 5xx errors.

Examples:

begin
  client.post("/orders", json: { address: "..." })
rescue Oauth2ApiClient::ResponseError::NotFound => e
  e.response # => HTTP::Response
rescue Oauth2ApiClient::ResponseError::BadRequest => e
  # ...
rescue Oauth2ApiClient::ResponseError => e
  # ...
end

Constant Summary collapse

STATUSES =
{
  400 => "Bad Request",
  401 => "Unauthorized",
  402 => "Payment Required",
  403 => "Forbidden",
  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 => "I'm A Teapot",
  421 => "Too Many Connections From This IP",
  422 => "Unprocessable Entity",
  423 => "Locked",
  424 => "Failed Dependency",
  425 => "Unordered Collection",
  426 => "Upgrade Required",
  428 => "Precondition Required",
  429 => "Too Many Requests",
  431 => "Request Header Fields Too Large",
  449 => "Retry With",
  450 => "Blocked By Windows Parental Controls",

  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",
  509 => "Bandwidth Limit Exceeded",
  510 => "Not Extended",
  511 => "Network Authentication Required"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResponseError

Returns a new instance of ResponseError.



67
68
69
# File 'lib/oauth2_api_client/response_error.rb', line 67

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



65
66
67
# File 'lib/oauth2_api_client/response_error.rb', line 65

def response
  @response
end

Class Method Details

.const_name(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a sanitized version to be used as a constant name for a given http error message.



90
91
92
# File 'lib/oauth2_api_client/response_error.rb', line 90

def self.const_name(message)
  message.gsub(/[^a-zA-Z0-9]/, "")
end

.for(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the exception class for a status code of the given response.



79
80
81
82
83
# File 'lib/oauth2_api_client/response_error.rb', line 79

def self.for(response)
  return const_get(const_name(STATUSES[response.code])).new(response) if STATUSES.key?(response.code)

  new(response)
end

Instance Method Details

#to_sObject



71
72
73
# File 'lib/oauth2_api_client/response_error.rb', line 71

def to_s
  "#{self.class.name} (#{response.code}, #{response.uri}): #{response.body}"
end