java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
is.codion.common.db.exception.DatabaseException
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AuthenticationException,DeleteEntityException,EntityNotFoundException,InsertEntityException,MultipleEntitiesFoundException,QueryTimeoutException,ReferentialIntegrityException,UniqueConstraintException,UpdateEntityException
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSQLException state indicating that a query did not return a result -
Constructor Summary
ConstructorsConstructorDescriptionDatabaseException(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.DatabaseException(@Nullable String message) Constructs a new DatabaseException instanceDatabaseException(@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.DatabaseException(SQLException cause) Constructs a new DatabaseException instanceDatabaseException(SQLException cause, @Nullable String message) Constructs a new DatabaseException instance -
Method Summary
Modifier and TypeMethodDescriptiondetail()Returns the error detail, such as the name of the constraint, column or table involved.final intReturns the underlying error code, note that this is only available when running with a local database connection.Returns the error type this exception is based on, if any.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, seemessage(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.final voidsetStackTrace(StackTraceElement[] stackTrace) sqlState()Returns the underlying sql state, note that this is only available when running with a local database connection.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, toString
-
Field Details
-
SQL_STATE_NO_DATA
SQLException state indicating that a query did not return a result- See Also:
-
-
Constructor Details
-
DatabaseException
Constructs a new DatabaseException instance- Parameters:
message- the exception message
-
DatabaseException
Constructs a new DatabaseException instance- Parameters:
cause- the root cause, the stack trace is copied and used
-
DatabaseException
Constructs a new DatabaseException instance- Parameters:
cause- the root cause, the stack trace is copied and usedmessage- the exception message
-
DatabaseException
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 typedetail- 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 usederrorType- the error typedetail- the detail, a constraint, column or table name for example, null if none- See Also:
-
-
Method Details
-
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
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
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, seemessage(Locale).- Overrides:
getMessagein classThrowable- Returns:
- the exception message
- See Also:
-
message
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
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
- Overrides:
setStackTracein classThrowable
-