Errors

The SDK defines specific error classes for different failure scenarios. All error classes extend the base SurrealError class, allowing you to catch and handle specific error types with isinstance checks.

Source: errors.py

Base Error

SurrealError

Base class for every error raised by the SurrealDB Python SDK. Extends Python's built-in Exception.

from surrealdb import SurrealError

try:
result = db.select("users")
except SurrealError as e:
print("SDK error:", e)

Server Errors

Server errors originate from the SurrealDB server and carry structured information about the failure. All server errors extend ServerError.

ServerError

Extends: SurrealError

Error received from the SurrealDB server with structured kind, details, and cause chain.

Properties

PropertyTypeDescription
kindstrThe structured error kind (e.g. "NotAllowed", "NotFound"). Match against ErrorKind constants.
codeintLegacy JSON-RPC error code. 0 when unavailable.
detailsdict[str, Any] | NoneKind-specific structured details. None when not provided.
server_causeServerError | NoneThe underlying server error in the cause chain, if any.

Methods

.has_kind()

Check if this error or any cause in the chain matches the given kind.

has_kind(kind: str) -> bool
from surrealdb import ErrorKind

try:
db.query("INVALID")
except ServerError as e:
if e.has_kind(ErrorKind.VALIDATION):
print("Validation error")

.find_cause()

Find the first error in the cause chain matching the given kind.

find_cause(kind: str) -> ServerError | None
try:
db.signin({"username": "user", "password": "wrong"})
except ServerError as e:
auth_cause = e.find_cause(ErrorKind.NOT_ALLOWED)
if auth_cause:
print("Auth failure:", auth_cause)

ValidationError

Extends: ServerError

Validation failure such as parse errors, invalid request parameters, or bad input.

Properties:

PropertyTypeDescription
is_parse_errorboolWhether this is a query parse error
parameter_namestr \| NoneThe invalid parameter name, if applicable

ConfigurationError

Extends: ServerError

Feature or configuration not supported, such as live queries or GraphQL.

Properties:

PropertyTypeDescription
is_live_query_not_supportedboolWhether live queries are not supported

ThrownError

Extends: ServerError

User-thrown error via the THROW statement in SurrealQL.

QueryError

Extends: ServerError

Query execution failure such as timeout, cancellation, or not executed.

Properties:

PropertyTypeDescription
is_not_executedboolWhether the query was not executed
is_timed_outboolWhether the query timed out
is_cancelledboolWhether the query was cancelled
timeoutdict[str, Any] \| NoneThe timeout duration as {"secs": ..., "nanos": ...}

SerializationError

Extends: ServerError

Serialization or deserialization failure.

Properties:

PropertyTypeDescription
is_deserializationboolWhether this is a deserialization error

NotAllowedError

Extends: ServerError

Permission denied, method not allowed, or function/scripting blocked.

Properties:

PropertyTypeDescription
is_token_expiredboolWhether the authentication token has expired
is_invalid_authboolWhether the authentication credentials are invalid
is_scripting_blockedboolWhether scripting is blocked
method_namestr \| NoneThe disallowed method name
function_namestr \| NoneThe disallowed function name
target_namestr \| NoneThe disallowed target name
from surrealdb import NotAllowedError

try:
db.signin({"username": "user", "password": "wrong"})
except NotAllowedError as e:
if e.is_token_expired:
print("Token expired, re-authenticate")
elif e.is_invalid_auth:
print("Invalid credentials")

NotFoundError

Extends: ServerError

Resource not found such as table, record, namespace, or method.

Properties:

PropertyTypeDescription
table_namestr \| NoneThe missing table name
record_idstr \| NoneThe missing record ID
method_namestr \| NoneThe missing method name
namespace_namestr \| NoneThe missing namespace name
database_namestr \| NoneThe missing database name
session_idstr \| NoneThe missing session ID

AlreadyExistsError

Extends: ServerError

Duplicate resource such as record, table, or namespace.

Properties:

PropertyTypeDescription
record_idstr \| NoneThe duplicate record ID
table_namestr \| NoneThe duplicate table name
session_idstr \| NoneThe duplicate session ID
namespace_namestr \| NoneThe duplicate namespace name
database_namestr \| NoneThe duplicate database name

InternalError

Extends: ServerError

Internal or unexpected server error. Used as a fallback when the error kind is not recognized.

SDK-Side Errors

These errors originate from the SDK itself, not the server.

ConnectionUnavailableError

Extends: SurrealError

Thrown when attempting an operation without an active connection.

from surrealdb import Surreal, ConnectionUnavailableError

db = Surreal("ws://localhost:8000")

try:
db.select("users")
except ConnectionUnavailableError:
print("Not connected to database")

UnsupportedEngineError

Extends: SurrealError

Thrown when the URL protocol is not supported.

Properties:

PropertyTypeDescription
urlstrThe unsupported URL
from surrealdb import Surreal, UnsupportedEngineError

try:
db = Surreal("ftp://localhost:8000")
except UnsupportedEngineError as e:
print(f"Unsupported protocol: {e.url}")

UnsupportedFeatureError

Extends: SurrealError

Thrown when a feature is not supported by the current connection type. For example, sessions and transactions require a WebSocket connection.

from surrealdb import AsyncSurreal, UnsupportedFeatureError

db = AsyncSurreal("http://localhost:8000")
await db.connect()

try:
session = await db.new_session()
except UnsupportedFeatureError:
print("Sessions require a WebSocket connection")

UnexpectedResponseError

Extends: SurrealError

Thrown when the server returns an unexpected response format.

InvalidRecordIdError

Extends: SurrealError

Thrown when a RecordID string could not be parsed.

InvalidDurationError

Extends: SurrealError

Thrown when a Duration string could not be parsed.

InvalidGeometryError

Extends: SurrealError

Thrown when geometry data is invalid.

InvalidTableError

Extends: SurrealError

Thrown when a table or record ID string is invalid.

Error Kind Constants

ErrorKind

Known error kinds returned by the SurrealDB server. Use these constants for matching against ServerError.kind.

from surrealdb import ErrorKind
ConstantValue
ErrorKind.VALIDATION"Validation"
ErrorKind.CONFIGURATION"Configuration"
ErrorKind.THROWN"Thrown"
ErrorKind.QUERY"Query"
ErrorKind.SERIALIZATION"Serialization"
ErrorKind.NOT_ALLOWED"NotAllowed"
ErrorKind.NOT_FOUND"NotFound"
ErrorKind.ALREADY_EXISTS"AlreadyExists"
ErrorKind.CONNECTION"Connection"
ErrorKind.INTERNAL"Internal"

Detail Kind Constants

Each server error kind has associated detail kind constants for more specific matching.

ClassConstants
AuthDetailKindTOKEN_EXPIRED, SESSION_EXPIRED, INVALID_AUTH, UNEXPECTED_AUTH, MISSING_USER_OR_PASS, NO_SIGNIN_TARGET, INVALID_PASS, TOKEN_MAKING_FAILED, INVALID_SIGNUP, INVALID_ROLE, NOT_ALLOWED
ValidationDetailKindPARSE, INVALID_REQUEST, INVALID_PARAMS, NAMESPACE_EMPTY, DATABASE_EMPTY, INVALID_PARAMETER, INVALID_CONTENT, INVALID_MERGE
ConfigurationDetailKindLIVE_QUERY_NOT_SUPPORTED, BAD_LIVE_QUERY_CONFIG, BAD_GRAPHQL_CONFIG
QueryDetailKindNOT_EXECUTED, TIMED_OUT, CANCELLED
SerializationDetailKindSERIALIZATION, DESERIALIZATION
NotAllowedDetailKindSCRIPTING, AUTH, METHOD, FUNCTION, TARGET
NotFoundDetailKindMETHOD, SESSION, TABLE, RECORD, NAMESPACE, DATABASE, TRANSACTION
AlreadyExistsDetailKindSESSION, TABLE, RECORD, NAMESPACE, DATABASE
ConnectionDetailKindUNINITIALISED, ALREADY_CONNECTED

See Also