Exception: Notion::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/notion/errors.rb

Constant Summary collapse

ERROR_CODES =
{
  "invalid_json" => InvalidJSONError,
  "invalid_request_url" => InvalidRequestURLError,
  "invalid_request" => InvalidRequestError,
  "invalid_grant" => InvalidGrantError,
  "validation_error" => ValidationError,
  "missing_version" => MissingVersionError,
  "invalid_beta" => InvalidBetaError,
  "unauthorized" => UnauthorizedError,
  "restricted_resource" => RestrictedResourceError,
  "object_not_found" => ObjectNotFoundError,
  "conflict_error" => ConflictError,
  "rate_limited" => RateLimitedError,
  "internal_server_error" => InternalServerError,
  "bad_gateway" => BadGatewayError,
  "service_unavailable" => ServiceUnavailableError,
  "database_connection_unavailable" => DatabaseConnectionUnavailableError,
  "gateway_timeout" => GatewayTimeoutError,
  "service_overload" => ServiceOverloadError
}.freeze
STATUS_ERRORS =
{
  400 => BadRequestError,
  401 => UnauthorizedError,
  403 => RestrictedResourceError,
  404 => ObjectNotFoundError,
  409 => ConflictError,
  429 => RateLimitedError,
  500 => InternalServerError,
  502 => BadGatewayError,
  503 => ServiceUnavailableError,
  504 => GatewayTimeoutError,
  529 => ServiceOverloadError
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ APIError

Returns a new instance of APIError.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/notion/errors.rb', line 20

def initialize(response)
  unless response.respond_to?(:body)
    super(response.to_s)
    return
  end

  body = response.body.is_a?(Hash) ? response.body : {}
  @status = response.status
  @code = body["code"]
  @request_id = response.request_id || body["request_id"]
  @additional_data = body["additional_data"]
  @retry_after = response.headers["retry-after"]&.to_f
  @response = response
  super(body["message"] || "Notion API request failed with status #{@status}")
end

Instance Attribute Details

#additional_dataObject (readonly)

Returns the value of attribute additional_data.



18
19
20
# File 'lib/notion/errors.rb', line 18

def additional_data
  @additional_data
end

#codeObject (readonly)

Returns the value of attribute code.



18
19
20
# File 'lib/notion/errors.rb', line 18

def code
  @code
end

#request_idObject (readonly)

Returns the value of attribute request_id.



18
19
20
# File 'lib/notion/errors.rb', line 18

def request_id
  @request_id
end

#responseObject (readonly)

Returns the value of attribute response.



18
19
20
# File 'lib/notion/errors.rb', line 18

def response
  @response
end

#retry_afterObject (readonly)

Returns the value of attribute retry_after.



18
19
20
# File 'lib/notion/errors.rb', line 18

def retry_after
  @retry_after
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/notion/errors.rb', line 18

def status
  @status
end

Class Method Details

.from_response(response) ⇒ Object



40
41
42
43
# File 'lib/notion/errors.rb', line 40

def self.from_response(response)
  code = response.body.is_a?(Hash) && response.body["code"]
  (ERROR_CODES[code] || STATUS_ERRORS[response.status] || self).new(response)
end

Instance Method Details

#retryable?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/notion/errors.rb', line 36

def retryable?
  status == 429 || status == 529 || status.between?(500, 599)
end