Errors

The Java SDK uses a hierarchy of exceptions rooted at SurrealException. Server-returned errors are represented by ServerException and its subclasses, which provide structured access to error details, kinds, and cause chains.

Source: surrealdb.java

Exception Hierarchy

  • SurrealException (extends RuntimeException)

    • ServerException

      • NotFoundException

      • NotAllowedException

      • QueryException

      • AlreadyExistsException

      • ValidationException

      • ConfigurationException

      • SerializationException

      • InternalException

      • ThrownException

SurrealException

Base exception for all SDK errors. Extends RuntimeException.

All exceptions thrown by the Java SDK are subclasses of SurrealException, making it possible to catch all SDK-related errors with a single catch block.

Example

try {
db.query("SELECT * FROM users");
} catch (SurrealException e) {
System.err.println("SDK error: " + e.getMessage());
}

ServerException

Base class for all server-returned errors. Extends SurrealException.

ServerException provides structured access to the error kind, details, and cause chain returned by the server. All specific server error types extend this class.

.getKind()

Returns the error kind as a string.

Method Syntax

exception.getKind()

Returns: String

Example

try {
db.select(Person.class, new RecordId("person", "missing"));
} catch (ServerException e) {
String kind = e.getKind();
}

.getKindEnum()

Returns the error kind as an ErrorKind enum value.

Method Syntax

exception.getKindEnum()

Returns: ErrorKind

Example

try {
db.query("INVALID QUERY");
} catch (ServerException e) {
ErrorKind kind = e.getKindEnum();
}

.getDetails()

Returns structured error details provided by the server.

Method Syntax

exception.getDetails()

Returns: Object

.getServerCause()

Returns the typed server cause if the error was caused by another server error.

Method Syntax

exception.getServerCause()

Returns: ServerException

.hasKind(kind)

Checks if the error or any error in its cause chain matches a specific kind.

Method Syntax

exception.hasKind(kind)
ParameterTypeDescription
kind String or ErrorKindThe error kind to check for.

Returns: boolean

Example

try {
db.query("SELECT * FROM protected_table");
} catch (ServerException e) {
if (e.hasKind(ErrorKind.NOT_ALLOWED)) {
System.err.println("Permission denied");
}
}

.findCause(kind)

Finds the first error in the cause chain that matches a specific kind.

Method Syntax

exception.findCause(kind)
ParameterTypeDescription
kind String or ErrorKindThe error kind to search for.

Returns: ServerException

Example

try {
db.query("CREATE person SET name = 'Alice'");
} catch (ServerException e) {
ServerException cause = e.findCause(ErrorKind.ALREADY_EXISTS);
if (cause != null) {
System.err.println("Duplicate: " + cause.getMessage());
}
}

ErrorKind

Enum representing error categories returned by the server.

ValueDescription
VALIDATIONData validation failed
CONFIGURATIONConfiguration error
THROWNExplicitly thrown error from SurrealQL
QUERYQuery execution error
SERIALIZATIONSerialization/deserialization error
NOT_ALLOWEDOperation not permitted
NOT_FOUNDResource not found
ALREADY_EXISTSResource already exists
CONNECTIONConnection error
INTERNALInternal server error
UNKNOWNUnknown error kind

ErrorKind.fromString(kind)

Converts a string to an ErrorKind enum value. Returns UNKNOWN if the string does not match any known kind.

Method Syntax

ErrorKind.fromString(kind)
ParameterTypeDescription
kind StringThe error kind string to convert.

Returns: ErrorKind

Example

ErrorKind kind = ErrorKind.fromString("NotFound");

NotFoundException

Thrown when a requested resource does not exist. Extends ServerException.

Additional Methods

MethodReturnsDescription
.getTableName()StringThe table that was queried
.getRecordId()StringThe record ID that was not found
.getMethodName()StringThe method that triggered the error
.getNamespaceName()StringThe namespace that was not found
.getDatabaseName()StringThe database that was not found
.getSessionId()StringThe session ID that was not found

Example

try {
db.select(Person.class, new RecordId("person", "nonexistent"));
} catch (NotFoundException e) {
String table = e.getTableName();
String record = e.getRecordId();
}

NotAllowedException

Thrown when an operation is not permitted. Extends ServerException.

Additional Methods

MethodReturnsDescription
.isTokenExpired()booleanWhether the authentication token has expired
.isInvalidAuth()booleanWhether the authentication credentials are invalid
.isScriptingBlocked()booleanWhether scripting is disabled on the server
.getMethodName()StringThe method that was not allowed
.getFunctionName()StringThe function that was not allowed
.getTargetName()StringThe target resource that was not accessible

Example

try {
db.query("SELECT * FROM protected_table");
} catch (NotAllowedException e) {
if (e.isTokenExpired()) {
db.signin(new RootCredential("root", "root"));
}
}

QueryException

Thrown when a query fails to execute. Extends ServerException.

Additional Methods

MethodReturnsDescription
.isNotExecuted()booleanWhether the query was not executed
.isTimedOut()booleanWhether the query timed out
.isCancelled()booleanWhether the query was cancelled
.getTimeout()Map<String, Object>The timeout details if the query timed out

Example

try {
db.query("SELECT * FROM large_table TIMEOUT 1s");
} catch (QueryException e) {
if (e.isTimedOut()) {
System.err.println("Query timed out: " + e.getTimeout());
}
}

AlreadyExistsException

Thrown when attempting to create a resource that already exists. Extends ServerException.

Additional Methods

MethodReturnsDescription
.getRecordId()StringThe record ID that already exists
.getTableName()StringThe table containing the duplicate
.getSessionId()StringThe session ID that already exists
.getNamespaceName()StringThe namespace that already exists
.getDatabaseName()StringThe database that already exists

Example

try {
db.create(Person.class, new RecordId("person", "alice"), person);
} catch (AlreadyExistsException e) {
String recordId = e.getRecordId();
}

ValidationException

Thrown when data validation fails. Extends ServerException. No additional methods.

ConfigurationException

Thrown when there is a configuration error. Extends ServerException. No additional methods.

SerializationException

Thrown when serialization or deserialization fails. Extends ServerException. No additional methods.

InternalException

Thrown for internal server errors. Extends ServerException. No additional methods.

ThrownException

Thrown when a SurrealQL THROW statement is executed. Extends ServerException. No additional methods.

Example

try {
db.query("THROW 'custom error message'");
} catch (ThrownException e) {
System.err.println("SurrealQL threw: " + e.getMessage());
}

See Also