module Attio::ErrorFactory
def self.from_exception(exception, context = {})
-
(Error)
- Appropriate error instance based on exception type
Parameters:
-
context
(Hash
) -- Additional context (currently unused) -
exception
(Exception
) -- The caught exception
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
def self.from_response(response, message = nil)
-
(Error)
- Appropriate error instance based on status code
Parameters:
-
message
(String, nil
) -- Optional custom error message -
response
(Hash
) -- Response hash with :status, :body, and :headers
def self.from_response(response, message = nil) status = response[:status].to_i message ||= "API request failed with status #{status}" case status when 400 then BadRequestError.new(message, response) when 401 then AuthenticationError.new(message, response) when 403 then ForbiddenError.new(message, response) when 404 then NotFoundError.new(message, response) when 409 then ConflictError.new(message, response) when 422 then UnprocessableEntityError.new(message, response) when 429 then RateLimitError.new(message, response) when 400..499 then ClientError.new(message, response) when 500..599 then ServerError.new(message, response) else Error.new(message, response) end end