class Teamsupport::Error

Custom error class for rescuing from all Teamsupport errors

Constants

BadGateway

Raised when Teamsupport returns the HTTP status code 502

BadRequest

Raised when Teamsupport returns the HTTP status code 400

ClientError

Raised when Teamsupport returns a 4xx HTTP status code

ERRORS
Forbidden

Raised when Teamsupport returns the HTTP status code 403

GatewayTimeout

Raised when Teamsupport returns the HTTP status code 504

InternalServerError

Raised when Teamsupport returns the HTTP status code 500

NotAcceptable

Raised when Teamsupport returns the HTTP status code 406

NotFound

Raised when Teamsupport returns the HTTP status code 404

ServerError

Raised when Teamsupport returns a 5xx HTTP status code

ServiceUnavailable

Raised when Teamsupport returns the HTTP status code 503

TooManyRequests

Raised when Teamsupport returns the HTTP status code 429

Unauthorized

Raised when Teamsupport returns the HTTP status code 401

UnprocessableEntity

Raised when Teamsupport returns the HTTP status code 422

Attributes

code[R]

Provide a code method for reading HTTP status code from Error

@example

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

@return [Integer]

@api public

Public Class Methods

from_response(body, _headers) click to toggle source

Create a new error from an HTTP response

@example

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

@param body [String] @param headers [Hash]

@return [Teamsupport::Error]

@api public

# File lib/teamsupport/error.rb, line 81
def from_response(body, _headers)
  message, code = parse_error(body)
  new(message, code)
end
new(message = '', code = nil) click to toggle source

Initializes a new Error object

@param message [Exception, String] @param code [Integer]

@return [Teamsupport::Error]

@api private

Calls superclass method
# File lib/teamsupport/error.rb, line 130
def initialize(message = '', code = nil)
  super(message)
  @code = code
end

Private Class Methods

extract_message_from_errors(body) click to toggle source

Extracts error messages from response body

@param body [String]

@return [Array]

@api private

# File lib/teamsupport/error.rb, line 112
def extract_message_from_errors(body)
  first = Array(body[:errors]).first
  if first.is_a?(Hash)
    [first[:message].chomp, first[:code]]
  else
    [first.chomp, nil]
  end
end
parse_error(body) click to toggle source

Parses response body for errors

@param body [String]

@return [Array, nil]

@api private

# File lib/teamsupport/error.rb, line 95
def parse_error(body)
  if body.nil? || body.empty?
    ['', nil]
  elsif body[:error]
    [body[:error], nil]
  elsif body[:errors]
    extract_message_from_errors(body)
  end
end