Module: Pinterest::Errors

Defined in:
lib/pinterest/errors.rb

Overview

A list of errors returned by the client.

Defined Under Namespace

Classes: AuthorizationError, BadRequestError, BaseError, MethodNotAllowedError, NotFoundError, NotImplementedError, PermissionsError, RateLimitError, ServerError, TimeoutError

Constant Summary

CODES =

A dictionary that associates HTTP status codes to error classes.

{
  400 => "BadRequestError",
  401 => "AuthorizationError",
  403 => "PermissionsError",
  404 => "NotFoundError",
  405 => "MethodNotAllowedError",
  408 => "TimeoutError",
  429 => "RateLimitError",
  501 => "NotImplementedError"
}.freeze

Class Method Summary collapse

Class Method Details

.class_for_code(status) ⇒ Class

Get the class for a HTTP status code.

Parameters:

  • status (Fixnum)

    The HTTP status code.

Returns:

  • (Class)

    The class.



89
90
91
92
# File 'lib/pinterest/errors.rb', line 89

def self.class_for_code(status)
  klass = ::Pinterest::Errors::CODES.fetch(status, "ServerError")
  Object.const_get("::Pinterest::Errors::#{klass}")
end

.create(response) ⇒ BaseError

Creates a new error.

Parameters:

  • response (Faraday::Response)

    The network response.

Returns:



78
79
80
81
82
83
# File 'lib/pinterest/errors.rb', line 78

def self.create(response)
  status = response.status
  message = response.body["error"] || response.body["message"]

  class_for_code(status).new(status, message, response.env)
end