Module: Datacentred::Errors

Defined in:
lib/datacentred/error.rb

Overview

Behaviours and exceptions for recoverable errors.

Defined Under Namespace

Classes: Error, NotFound, Unauthorized, UnprocessableEntity

Class Method Summary collapse

Class Method Details

.raise_unless_successful(status, body) ⇒ nil

Test server response and raise appropriate error if an error has been returned.

Returns:

  • (nil)

    Returns nil on success

Raises:

  • (Error)

    Appropriate error for server response code.



8
9
10
11
12
13
14
15
16
17
# File 'lib/datacentred/error.rb', line 8

def self.raise_unless_successful(status, body)
  return if status.to_s.start_with? "2" # 2xx
  err = errors[status]
  message = body&.fetch("errors")&.first&.fetch("detail")
  if err
    raise err, message || status.to_s
  else
    raise Error, "Error #{status}: #{message}"
  end
end