Exception: Teamsupport::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/teamsupport/error.rb

Overview

Custom error class for rescuing from all Teamsupport errors

Constant Summary collapse

ClientError =

Raised when Teamsupport returns a 4xx HTTP status code

Class.new(self)
BadRequest =

Raised when Teamsupport returns the HTTP status code 400

Class.new(ClientError)
Unauthorized =

Raised when Teamsupport returns the HTTP status code 401

Class.new(ClientError)
Forbidden =

Raised when Teamsupport returns the HTTP status code 403

Class.new(ClientError)
NotFound =

Raised when Teamsupport returns the HTTP status code 404

Class.new(ClientError)
NotAcceptable =

Raised when Teamsupport returns the HTTP status code 406

Class.new(ClientError)
UnprocessableEntity =

Raised when Teamsupport returns the HTTP status code 422

Class.new(ClientError)
TooManyRequests =

Raised when Teamsupport returns the HTTP status code 429

Class.new(ClientError)
ServerError =

Raised when Teamsupport returns a 5xx HTTP status code

Class.new(self)
InternalServerError =

Raised when Teamsupport returns the HTTP status code 500

Class.new(ServerError)
BadGateway =

Raised when Teamsupport returns the HTTP status code 502

Class.new(ServerError)
ServiceUnavailable =

Raised when Teamsupport returns the HTTP status code 503

Class.new(ServerError)
GatewayTimeout =

Raised when Teamsupport returns the HTTP status code 504

Class.new(ServerError)
ERRORS =
{
  400 => Teamsupport::Error::BadRequest,
  401 => Teamsupport::Error::Unauthorized,
  403 => Teamsupport::Error::Forbidden,
  404 => Teamsupport::Error::NotFound,
  406 => Teamsupport::Error::NotAcceptable,
  422 => Teamsupport::Error::UnprocessableEntity,
  429 => Teamsupport::Error::TooManyRequests,
  500 => Teamsupport::Error::InternalServerError,
  502 => Teamsupport::Error::BadGateway,
  503 => Teamsupport::Error::ServiceUnavailable,
  504 => Teamsupport::Error::GatewayTimeout,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '', code = nil) ⇒ Teamsupport::Error

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.

Initializes a new Error object

Parameters:

  • message (Exception, String) (defaults to: '')
  • code (Integer) (defaults to: nil)


130
131
132
133
# File 'lib/teamsupport/error.rb', line 130

def initialize(message = '', code = nil)
  super(message)
  @code = code
end

Instance Attribute Details

#codeInteger (readonly)

Provide a code method for reading HTTP status code from Error

Examples:

teamsupport_error = Teamsupport::Error.new(code: 404)
teamsupport_error.code

Returns:

  • (Integer)


13
14
15
# File 'lib/teamsupport/error.rb', line 13

def code
  @code
end

Class Method Details

.from_response(body, _headers) ⇒ Teamsupport::Error

Create a new error from an HTTP response

Examples:

teamsupport_error = Teamsupport::Error::ERRORS[code]
teamsupport_error.from_response(body, headers) unless klass.nil?

Parameters:

  • body (String)
  • headers (Hash)

Returns:



81
82
83
84
# File 'lib/teamsupport/error.rb', line 81

def from_response(body, _headers)
  message, code = parse_error(body)
  new(message, code)
end