class Fauna::FaunaError

Error returned by the FaunaDB server. For documentation of error types, see the docs.

Attributes

errors[R]

List of ErrorData objects returned by the server.

request_result[R]

RequestResult for the request that caused this error.

Public Class Methods

new(request_result) click to toggle source

Creates a new error from a given RequestResult or Exception.

Calls superclass method
# File lib/fauna/errors.rb, line 63
def initialize(request_result)
  message = nil

  if request_result.is_a? RequestResult
    @request_result = request_result

    begin
      if request_result.response_content.nil?
        fail UnexpectedError.new('Invalid JSON.', request_result)
      end

      errors_raw = UnexpectedError.get_or_raise request_result, request_result.response_content, :errors
      @errors = catch :invalid_response do
        throw :invalid_response unless errors_raw.is_a? Array
        errors_raw.map { |error| ErrorData.from_hash(error) }
      end

      if @errors.nil?
        fail UnexpectedError.new('Error data has an unexpected format.', request_result)
      elsif @errors.empty?
        fail UnexpectedError.new('Error data returned was blank.', request_result)
      end

      message = @errors.map do |error|
        msg = 'Error'
        msg += " at #{error.position}" unless error.position.nil?
        msg += ": #{error.code} - #{error.description}"

        unless error.failures.nil?
          msg += ' (' + error.failures.map do |failure|
            "Failure at #{failure.field}: #{failure.code} - #{failure.description}"
          end.join(' ') + ')'
        end

        msg
      end.join(' ')
    rescue UnexpectedError => e
      unless self.is_a?(UnavailableError) && [502, 503, 504].include?(request_result.status_code)
        raise e
      end

      message = request_result.response_raw
    end
  elsif request_result.is_a? Exception
    message = request_result.class.name
    unless request_result.message.nil?
      message += ": #{request_result.message}"
    end
  end

  super(message)
end
raise_for_status_code(request_result) click to toggle source

Raises the associated error from a RequestResult based on the status code.

Returns nil for 2xx status codes

# File lib/fauna/errors.rb, line 35
def self.raise_for_status_code(request_result)
  case request_result.status_code
  when 200..299

  when 400
    fail BadRequest.new(request_result)
  when 401
    fail Unauthorized.new(request_result)
  when 403
    fail PermissionDenied.new(request_result)
  when 404
    fail NotFound.new(request_result)
  when 405
    fail MethodNotAllowed.new(request_result)
  when 500
    fail InternalError.new(request_result)
  when 502
    fail UnavailableError.new(request_result)
  when 503
    fail UnavailableError.new(request_result)
  when 504
    fail UnavailableError.new(request_result)
  else
    fail UnexpectedError.new('Unexpected status code.', request_result)
  end
end