Exception: TheCity::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/the_city/error.rb,
lib/the_city/error/forbidden.rb,
lib/the_city/error/not_found.rb,
lib/the_city/error/bad_gateway.rb,
lib/the_city/error/bad_request.rb,
lib/the_city/error/unauthorized.rb,
lib/the_city/error/argument_arror.rb,
lib/the_city/error/not_acceptable.rb,
lib/the_city/error/gateway_timeout.rb,
lib/the_city/error/too_many_requests.rb,
lib/the_city/error/configuration_error.rb,
lib/the_city/error/service_unavailable.rb,
lib/the_city/error/unprocessable_entity.rb,
lib/the_city/error/internal_server_error.rb

Overview

Custom error class for rescuing from all The City API errors

Defined Under Namespace

Classes: ArgumentError, BadGateway, BadRequest, ConfigurationError, Forbidden, GatewayTimeout, InternalServerError, NotAcceptable, NotFound, ServiceUnavailable, TooManyRequests, Unauthorized, UnprocessableEntity

Constant Summary collapse

EnhanceYourCalm =
TooManyRequests
RateLimited =
TooManyRequests

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception = $!, response_headers = {}, code = nil) ⇒ TheCity::Error

Initializes a new Error object

Parameters:

  • exception (Exception, String) (defaults to: $!)
  • response_headers (Hash) (defaults to: {})
  • code (Integer) (defaults to: nil)


41
42
43
44
45
46
# File 'lib/the_city/error.rb', line 41

def initialize(exception=$!, response_headers={}, code=nil)
  @rate_limit = TheCity::RateLimit.new(response_headers)
  @wrapped_exception = exception
  @code = code
  exception.respond_to?(:message) ? super(exception.message) : super(exception.to_s)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/the_city/error.rb', line 6

def code
  @code
end

#rate_limitObject (readonly)

Returns the value of attribute rate_limit.



6
7
8
# File 'lib/the_city/error.rb', line 6

def rate_limit
  @rate_limit
end

#wrapped_exceptionObject (readonly)

Returns the value of attribute wrapped_exception.



6
7
8
# File 'lib/the_city/error.rb', line 6

def wrapped_exception
  @wrapped_exception
end

Class Method Details

.descendantsArray

Returns:

  • (Array)


26
27
28
# File 'lib/the_city/error.rb', line 26

def self.descendants
  @descendants ||= []
end

.errorsHash

Returns:

  • (Hash)


18
19
20
21
22
23
# File 'lib/the_city/error.rb', line 18

def self.errors
  @errors ||= descendants.inject({}) do |hash, klass|
    hash[klass::HTTP_STATUS_CODE] = klass
    hash
  end
end

.from_response(response = {}) ⇒ TheCity::Error

Create a new error from an HTTP response

Parameters:

  • response (Hash) (defaults to: {})

Returns:



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

def self.from_response(response={})
  error, code = parse_error(response[:body])
  new(error, response[:response_headers], code)
end

.inherited(descendant) ⇒ Array

Returns:

  • (Array)


31
32
33
# File 'lib/the_city/error.rb', line 31

def self.inherited(descendant)
  descendants << descendant
end