Warning:
Fauna is decommissioning FQL v4 on June 30, 2025.

This driver is not compatible with FQL v10, the latest version. Fauna accounts created after August 21, 2024 must use FQL v10.
Ensure you migrate existing projects to the official v10 driver by the v4 EOL date: https://github.com/fauna/fauna-python.

For more information, see the v4 end of life (EOL) announcement and related FAQ.

Module faunadb.errors

Error types that methods in the FaunaDB client throw.

Expand source code
"""Error types that methods in the FaunaDB client throw."""
# pylint: disable=redefined-builtin
from builtins import object

from requests import codes

def _get_or_raise(request_result, dct, key):
  if isinstance(dct, dict) and key in dct:
    return dct[key]
  else:
    raise UnexpectedError("Response JSON does not contain expected key %s" % key, request_result)

#region FaunaError

class FaunaError(Exception):
  """
  Error returned by the FaunaDB server.
  For documentation of error types, see the `docs <https://fauna.com/documentation#errors>`__.
  """

  @staticmethod
  def raise_for_status_code(request_result):
    code = request_result.status_code
    # pylint: disable=no-member, too-many-return-statements
    if 200 <= code <= 299:
      pass
    elif code == codes.bad_request:
      raise BadRequest(request_result)
    elif code == codes.unauthorized:
      raise Unauthorized(request_result)
    elif code == codes.forbidden:
      raise PermissionDenied(request_result)
    elif code == codes.not_found:
      raise NotFound(request_result)
    elif code == codes.internal_server_error:
      raise InternalError(request_result)
    elif code == codes.unavailable:
      raise UnavailableError(request_result)
    else:
      raise UnexpectedError("Unexpected status code.", request_result)

  def __init__(self, description, request_result):
    super(FaunaError, self).__init__(description)
    self.request_result = request_result
    """:any:`RequestResult` for the request that caused this error."""


class UnexpectedError(FaunaError):
  """Error for when the server returns an unexpected kind of response."""
  pass


class HttpError(FaunaError):
  def __init__(self, request_result):
    self.errors = HttpError._get_errors(request_result)
    """List of all :py:class:`ErrorData` objects sent by the server."""
    super(HttpError, self).__init__(self._get_description(), request_result)

  @staticmethod
  def _get_errors(request_result):
    response = request_result.response_content
    errors = _get_or_raise(request_result, response, "errors")
    return [ErrorData.from_dict(error, request_result) for error in errors]

  def _get_description(self):
    return self.errors[0].description if self.errors else "(empty `errors`)"


class BadRequest(HttpError):
  """HTTP 400 error."""
  pass


class Unauthorized(HttpError):
  """HTTP 401 error."""
  pass


class PermissionDenied(HttpError):
  """HTTP 403 error."""
  pass


class NotFound(HttpError):
  """HTTP 404 error."""
  pass


class InternalError(HttpError):
  """HTTP 500 error."""
  pass


class UnavailableError(HttpError):
  """HTTP 503 error."""
  pass


#endregion

class ErrorData(object):
  """
  Data for one error returned by the server.
  """

  @staticmethod
  def from_dict(dct, request_result):
    return ErrorData(
      _get_or_raise(request_result, dct, "code"),
      _get_or_raise(request_result, dct, "description"),
      dct.get("position"),
      ErrorData.get_failures(dct, request_result))

  @staticmethod
  def get_failures(dct, request_result):
    if "failures" in dct:
      return [Failure.from_dict(failure, request_result) for failure in dct["failures"]]
    return None

  def __init__(self, code, description, position, failures):
    self.code = code
    """Error code. See all error codes `here <https://fauna.com/documentation#errors>`__."""
    self.description = description
    """Error description."""
    self.position = position
    """Position of the error in a query. May be None."""
    self.failures = failures
    """
    List of all :py:class:`Failure` objects returned by the server.
    None unless code == "validation failed".
    """

  def __repr__(self):
    return "ErrorData(%s, %s, %s, %s)" % \
           (repr(self.code), repr(self.description), repr(self.position), repr(self.failures))

  def __eq__(self, other):
    return self.__class__ == other.__class__ and \
      self.description == other.description and \
      self.position == other.position and \
      self.failures == other.failures

  def __ne__(self, other):
    # pylint: disable=unneeded-not
    return not self == other

class Failure(object):
  """
  Part of the ``failures`` of an :py:class:`ErrorData`.
  See the ``Invalid Data`` section of the `docs <https://fauna.com/documentation#errors>`__.
  """

  @staticmethod
  def from_dict(dct, request_result):
    return Failure(
      _get_or_raise(request_result, dct, "code"),
      _get_or_raise(request_result, dct, "description"),
      _get_or_raise(request_result, dct, "field"))

  def __init__(self, code, description, field):
    self.code = code
    """Failure code."""
    self.description = description
    """Failure description."""
    self.field = field
    """Field of the failure in the instance."""

  def __repr__(self):
    return "Failure(%s, %s, %s)" % (repr(self.code), repr(self.description), repr(self.field))

  def __eq__(self, other):
    return self.code == other.code and \
      self.description == other.description and \
      self.field == other.field

  def __ne__(self, other):
    # pylint: disable=unneeded-not
    return not self == other

Classes

class BadRequest (request_result)

HTTP 400 error.

Expand source code
class BadRequest(HttpError):
  """HTTP 400 error."""
  pass

Ancestors

Inherited members

class ErrorData (code, description, position, failures)

Data for one error returned by the server.

Expand source code
class ErrorData(object):
  """
  Data for one error returned by the server.
  """

  @staticmethod
  def from_dict(dct, request_result):
    return ErrorData(
      _get_or_raise(request_result, dct, "code"),
      _get_or_raise(request_result, dct, "description"),
      dct.get("position"),
      ErrorData.get_failures(dct, request_result))

  @staticmethod
  def get_failures(dct, request_result):
    if "failures" in dct:
      return [Failure.from_dict(failure, request_result) for failure in dct["failures"]]
    return None

  def __init__(self, code, description, position, failures):
    self.code = code
    """Error code. See all error codes `here <https://fauna.com/documentation#errors>`__."""
    self.description = description
    """Error description."""
    self.position = position
    """Position of the error in a query. May be None."""
    self.failures = failures
    """
    List of all :py:class:`Failure` objects returned by the server.
    None unless code == "validation failed".
    """

  def __repr__(self):
    return "ErrorData(%s, %s, %s, %s)" % \
           (repr(self.code), repr(self.description), repr(self.position), repr(self.failures))

  def __eq__(self, other):
    return self.__class__ == other.__class__ and \
      self.description == other.description and \
      self.position == other.position and \
      self.failures == other.failures

  def __ne__(self, other):
    # pylint: disable=unneeded-not
    return not self == other

Static methods

def from_dict(dct, request_result)
Expand source code
@staticmethod
def from_dict(dct, request_result):
  return ErrorData(
    _get_or_raise(request_result, dct, "code"),
    _get_or_raise(request_result, dct, "description"),
    dct.get("position"),
    ErrorData.get_failures(dct, request_result))
def get_failures(dct, request_result)
Expand source code
@staticmethod
def get_failures(dct, request_result):
  if "failures" in dct:
    return [Failure.from_dict(failure, request_result) for failure in dct["failures"]]
  return None

Instance variables

var code

Error code. See all error codes here <https://fauna.com/documentation#errors>__.

var description

Error description.

var failures

List of all :py:class:Failure objects returned by the server. None unless code == "validation failed".

var position

Position of the error in a query. May be None.

class Failure (code, description, field)

Part of the failures of an :py:class:ErrorData. See the Invalid Data section of the docs <https://fauna.com/documentation#errors>__.

Expand source code
class Failure(object):
  """
  Part of the ``failures`` of an :py:class:`ErrorData`.
  See the ``Invalid Data`` section of the `docs <https://fauna.com/documentation#errors>`__.
  """

  @staticmethod
  def from_dict(dct, request_result):
    return Failure(
      _get_or_raise(request_result, dct, "code"),
      _get_or_raise(request_result, dct, "description"),
      _get_or_raise(request_result, dct, "field"))

  def __init__(self, code, description, field):
    self.code = code
    """Failure code."""
    self.description = description
    """Failure description."""
    self.field = field
    """Field of the failure in the instance."""

  def __repr__(self):
    return "Failure(%s, %s, %s)" % (repr(self.code), repr(self.description), repr(self.field))

  def __eq__(self, other):
    return self.code == other.code and \
      self.description == other.description and \
      self.field == other.field

  def __ne__(self, other):
    # pylint: disable=unneeded-not
    return not self == other

Static methods

def from_dict(dct, request_result)
Expand source code
@staticmethod
def from_dict(dct, request_result):
  return Failure(
    _get_or_raise(request_result, dct, "code"),
    _get_or_raise(request_result, dct, "description"),
    _get_or_raise(request_result, dct, "field"))

Instance variables

var code

Failure code.

var description

Failure description.

var field

Field of the failure in the instance.

class FaunaError (description, request_result)

Error returned by the FaunaDB server. For documentation of error types, see the docs <https://fauna.com/documentation#errors>__.

Expand source code
class FaunaError(Exception):
  """
  Error returned by the FaunaDB server.
  For documentation of error types, see the `docs <https://fauna.com/documentation#errors>`__.
  """

  @staticmethod
  def raise_for_status_code(request_result):
    code = request_result.status_code
    # pylint: disable=no-member, too-many-return-statements
    if 200 <= code <= 299:
      pass
    elif code == codes.bad_request:
      raise BadRequest(request_result)
    elif code == codes.unauthorized:
      raise Unauthorized(request_result)
    elif code == codes.forbidden:
      raise PermissionDenied(request_result)
    elif code == codes.not_found:
      raise NotFound(request_result)
    elif code == codes.internal_server_error:
      raise InternalError(request_result)
    elif code == codes.unavailable:
      raise UnavailableError(request_result)
    else:
      raise UnexpectedError("Unexpected status code.", request_result)

  def __init__(self, description, request_result):
    super(FaunaError, self).__init__(description)
    self.request_result = request_result
    """:any:`RequestResult` for the request that caused this error."""

Ancestors

  • builtins.Exception
  • builtins.BaseException

Subclasses

Static methods

def raise_for_status_code(request_result)
Expand source code
@staticmethod
def raise_for_status_code(request_result):
  code = request_result.status_code
  # pylint: disable=no-member, too-many-return-statements
  if 200 <= code <= 299:
    pass
  elif code == codes.bad_request:
    raise BadRequest(request_result)
  elif code == codes.unauthorized:
    raise Unauthorized(request_result)
  elif code == codes.forbidden:
    raise PermissionDenied(request_result)
  elif code == codes.not_found:
    raise NotFound(request_result)
  elif code == codes.internal_server_error:
    raise InternalError(request_result)
  elif code == codes.unavailable:
    raise UnavailableError(request_result)
  else:
    raise UnexpectedError("Unexpected status code.", request_result)

Instance variables

var request_result

:any:RequestResult for the request that caused this error.

class HttpError (request_result)

Error returned by the FaunaDB server. For documentation of error types, see the docs <https://fauna.com/documentation#errors>__.

Expand source code
class HttpError(FaunaError):
  def __init__(self, request_result):
    self.errors = HttpError._get_errors(request_result)
    """List of all :py:class:`ErrorData` objects sent by the server."""
    super(HttpError, self).__init__(self._get_description(), request_result)

  @staticmethod
  def _get_errors(request_result):
    response = request_result.response_content
    errors = _get_or_raise(request_result, response, "errors")
    return [ErrorData.from_dict(error, request_result) for error in errors]

  def _get_description(self):
    return self.errors[0].description if self.errors else "(empty `errors`)"

Ancestors

  • FaunaError
  • builtins.Exception
  • builtins.BaseException

Subclasses

Instance variables

var errors

List of all :py:class:ErrorData objects sent by the server.

Inherited members

class InternalError (request_result)

HTTP 500 error.

Expand source code
class InternalError(HttpError):
  """HTTP 500 error."""
  pass

Ancestors

Inherited members

class NotFound (request_result)

HTTP 404 error.

Expand source code
class NotFound(HttpError):
  """HTTP 404 error."""
  pass

Ancestors

Inherited members

class PermissionDenied (request_result)

HTTP 403 error.

Expand source code
class PermissionDenied(HttpError):
  """HTTP 403 error."""
  pass

Ancestors

Inherited members

class Unauthorized (request_result)

HTTP 401 error.

Expand source code
class Unauthorized(HttpError):
  """HTTP 401 error."""
  pass

Ancestors

Inherited members

class UnavailableError (request_result)

HTTP 503 error.

Expand source code
class UnavailableError(HttpError):
  """HTTP 503 error."""
  pass

Ancestors

Inherited members

class UnexpectedError (description, request_result)

Error for when the server returns an unexpected kind of response.

Expand source code
class UnexpectedError(FaunaError):
  """Error for when the server returns an unexpected kind of response."""
  pass

Ancestors

  • FaunaError
  • builtins.Exception
  • builtins.BaseException

Inherited members