Module: Attio::ErrorFactory
- Defined in:
- lib/attio/errors.rb
Overview
Factory module for creating appropriate error instances
Class Method Summary collapse
-
.from_exception(exception, context = {}) ⇒ Error
Create an error instance from a caught exception.
-
.from_response(response, message = nil) ⇒ Error
Create an error instance from an HTTP response.
Class Method Details
.from_exception(exception, context = {}) ⇒ Error
Create an error instance from a caught exception
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/attio/errors.rb', line 113 def self.from_exception(exception, context = {}) case exception when Faraday::TimeoutError, Net::ReadTimeout, Net::OpenTimeout TimeoutError.new("Request timed out: #{exception.message}") when Faraday::ConnectionFailed, SocketError, Errno::ECONNREFUSED NetworkError.new("Network error: #{exception.message}") when Faraday::ClientError from_response({status: exception.response_status, body: exception.response_body}) else ConnectionError.new("Connection error: #{exception.message}") end end |
.from_response(response, message = nil) ⇒ Error
Create an error instance from an HTTP response
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/attio/errors.rb', line 90 def self.from_response(response, = nil) status = response[:status].to_i ||= "API request failed with status #{status}" case status when 400 then BadRequestError.new(, response) when 401 then AuthenticationError.new(, response) when 403 then ForbiddenError.new(, response) when 404 then NotFoundError.new(, response) when 409 then ConflictError.new(, response) when 422 then UnprocessableEntityError.new(, response) when 429 then RateLimitError.new(, response) when 400..499 then ClientError.new(, response) when 500..599 then ServerError.new(, response) else Error.new(, response) end end |