Module: Attio::ErrorFactory

Defined in:
lib/attio/errors.rb

Overview

Factory module for creating appropriate error instances

Class Method Summary collapse

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, 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