Class DatabaseException

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AuthenticationException, DeleteEntityException, EntityNotFoundException, InsertEntityException, MultipleEntitiesFoundException, QueryTimeoutException, ReferentialIntegrityException, UniqueConstraintException, UpdateEntityException

public class DatabaseException extends RuntimeException
An exception coming from a database-layer.

Note that the underlying SQLException is not preserved as the cause (Throwable.getCause() returns null); instead its stack trace is copied and its error code and sql state captured. This keeps the exception deserializable on a client without the database driver classes on its classpath.

An exception based on an ErrorType carries the error type and the detail, if any, instead of a message, the message being put together when read, see getMessage() and message(Locale). The message thereby follows the language of the one reading it, that of the client, not the server the exception came from.

See Also:
  • Field Details

    • SQL_STATE_NO_DATA

      public static final String SQL_STATE_NO_DATA
      SQLException state indicating that a query did not return a result
      See Also:
  • Constructor Details

    • DatabaseException

      public DatabaseException(@Nullable String message)
      Constructs a new DatabaseException instance
      Parameters:
      message - the exception message
    • DatabaseException

      public DatabaseException(SQLException cause)
      Constructs a new DatabaseException instance
      Parameters:
      cause - the root cause, the stack trace is copied and used
    • DatabaseException

      public DatabaseException(SQLException cause, @Nullable String message)
      Constructs a new DatabaseException instance
      Parameters:
      cause - the root cause, the stack trace is copied and used
      message - the exception message
    • DatabaseException

      public DatabaseException(ErrorType errorType, @Nullable String detail)
      Constructs a new DatabaseException instance, based on an error type, its message the one associated with the error type, along with the detail, if any.
      Parameters:
      errorType - the error type
      detail - the detail, a constraint, column or table name for example, null if none
      See Also:
    • DatabaseException

      public DatabaseException(@Nullable SQLException cause, ErrorType errorType, @Nullable String detail)
      Constructs a new DatabaseException instance, based on an error type, its message the one associated with the error type, along with the detail, if any.
      Parameters:
      cause - the root cause, the stack trace is copied and used
      errorType - the error type
      detail - the detail, a constraint, column or table name for example, null if none
      See Also:
  • Method Details

    • errorType

      public final Optional<ErrorType> errorType()
      Returns the error type this exception is based on, if any. Note that an exception coming from a newer server may be based on an error type not known to the client, in which case this is empty, the message then being the one put together by the server.
      Returns:
      the error type, an empty Optional if this exception is not based on a known error type
    • detail

      public final Optional<String> detail()
      Returns the error detail, such as the name of the constraint, column or table involved.
      Returns:
      the error detail, an empty Optional if none is available
    • getMessage

      public final @Nullable String getMessage()
      Returns the message, for an exception based on an error type the one associated with the error type, in the default locale of the one reading it, along with the detail, if any, see message(Locale).
      Overrides:
      getMessage in class Throwable
      Returns:
      the exception message
      See Also:
    • message

      public @Nullable String message(Locale locale)
      Returns the message in the given locale, for an exception based on an error type, otherwise the message this exception was constructed with, which is not affected by the locale. Useful where the default locale is not the one of the reader, a web server serving users of different languages for example.
      Parameters:
      locale - the locale
      Returns:
      the exception message in the given locale
    • errorCode

      public final int errorCode()
      Returns the underlying error code, note that this is only available when running with a local database connection.
      Returns:
      the underlying error code, -1 if not available
    • sqlState

      public final Optional<String> sqlState()
      Returns the underlying sql state, note that this is only available when running with a local database connection.
      Returns:
      the underlying sql state, an empty Optional if not available
    • setStackTrace

      public final void setStackTrace(StackTraceElement[] stackTrace)
      Overrides:
      setStackTrace in class Throwable