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.
Base Error
SurrealError
Base class for all SDK errors.
Extends: Error
Example:
Connection Errors
ConnectionUnavailableError
Thrown when attempting an operation without an active connection.
Message: "You must be connected to a SurrealDB instance before performing this operation"
Example:
HttpConnectionError
Thrown when an HTTP connection fails.
Properties:
status(number) - HTTP status codestatusText(string) - HTTP status textbuffer(ArrayBuffer) - Response buffer
Example:
CallTerminatedError
Thrown when a call is terminated because the connection was closed.
Message: "The call has been terminated because the connection was closed"
Example:
UnexpectedConnectionError
Thrown when an unexpected connection error occurs.
Properties:
cause(unknown) - The underlying error cause
Example:
UnsupportedEngineError
Thrown when attempting to use an unsupported or unconfigured engine.
Properties:
engine(string) - The unsupported engine name
Example:
Reconnection Errors
ReconnectExhaustionError
Thrown when reconnect attempts have been exhausted.
Message: "The reconnect attempts have been exhausted"
Example:
ReconnectIterationError
Thrown when a reconnect iterator fails to iterate.
Message: "The reconnect iterator failed to iterate"
Server Errors
Server errors represent structured errors returned by the SurrealDB server. They form a class hierarchy rooted at ServerError, which replaces the former ResponseError class.
ErrorKind
Known error kinds returned by the SurrealDB server. Use these constants for matching against ServerError.kind.
ServerError
Base class for all errors originating from the SurrealDB server. Each error carries structured information about the failure.
Extends: SurrealError
Properties:
kind(string) - The error category (e.g."NotAllowed","NotFound")code(number) - Legacy JSON-RPC numeric error code (0 when unavailable)details(ErrorDetail | undefined) - Kind-specific structured details from the servercause(ServerError | undefined) - Optional innerServerErrorforming a recursive error chain
Example:
ValidationError
Thrown on validation failures such as parse errors, invalid requests, or invalid parameters.
Extends: ServerError (kind: "Validation")
Convenience getters:
isParseError(boolean) - True if this is a SurrealQL parse errorparameterName(string | undefined) - The name of the invalid parameter, if applicable
Example:
ConfigurationError
Thrown when a feature or configuration is not supported by the server (e.g. live queries, GraphQL).
Extends: ServerError (kind: "Configuration")
Convenience getters:
isLiveQueryNotSupported(boolean) - True if live queries are not supported by the server configuration
ThrownError
Thrown when a user-thrown error is raised via THROW in SurrealQL.
Extends: ServerError (kind: "Thrown")
Example:
QueryError
Thrown on query execution failures such as timeouts, cancellations, or skipped statements.
Extends: ServerError (kind: "Query")
Convenience getters:
isNotExecuted(boolean) - True if the query was not executed (e.g. due to a prior error in the batch)isTimedOut(boolean) - True if the query timed outisCancelled(boolean) - True if the query was cancelledtimeout({ secs: number, nanos: number } | undefined) - The timeout duration, if this is a timeout error
Example:
SerializationError
Thrown on serialization or deserialization failures.
Extends: ServerError (kind: "Serialization")
Convenience getters:
isDeserialization(boolean) - True if this is a deserialization error (as opposed to serialization)
NotAllowedError
Thrown when a permission is denied, a method is not allowed, or a function/scripting call is blocked.
Extends: ServerError (kind: "NotAllowed")
Convenience getters:
isTokenExpired(boolean) - True if the auth token has expiredisInvalidAuth(boolean) - True if authentication credentials are invalidisScriptingBlocked(boolean) - True if scripting is blockedmethodName(string | undefined) - The method name that is not allowed, if applicablefunctionName(string | undefined) - The function name that is not allowed, if applicable
Example:
NotFoundError
Thrown when a resource is not found (table, record, namespace, method, etc.).
Extends: ServerError (kind: "NotFound")
Convenience getters:
tableName(string | undefined) - The table name that was not foundrecordId(string | undefined) - The record ID that was not foundmethodName(string | undefined) - The RPC method name that was not foundnamespaceName(string | undefined) - The namespace name that was not founddatabaseName(string | undefined) - The database name that was not found
Example:
AlreadyExistsError
Thrown when a duplicate resource is encountered (record, table, namespace, etc.).
Extends: ServerError (kind: "AlreadyExists")
Convenience getters:
recordId(string | undefined) - The record ID that already existstableName(string | undefined) - The table name that already exists
Example:
InternalError
Thrown on unexpected or unknown internal server errors. Also used as the fallback for unrecognized kind strings from newer servers.
Extends: ServerError (kind: "Internal")
ResponseError (Deprecated)
ResponseError is a deprecated alias for ServerError. It exists for backward compatibility.
UnexpectedServerResponseError
Thrown when the server returns a response in an unexpected format.
Properties:
response(unknown) - The unexpected response received
Example:
Authentication Errors
AuthenticationError
Thrown when authentication fails.
Message: "Authentication did not succeed"
Properties:
cause(unknown) - The underlying error cause
Example:
MissingNamespaceDatabaseError
Thrown when a namespace and/or database is required but not selected.
Message: "There is no namespace and/or database selected"
Example:
Live Query Errors
LiveSubscriptionError
Thrown when a live subscription fails to listen.
Constructor: new LiveSubscriptionError(messageOrCause?: string | unknown)
When called with a string, it is used as the error message. When called with any other value (or no argument), the default message "Live subscription failed to listen" is used and the argument is set as cause.
Example:
Version Errors
UnsupportedVersionError
Thrown when the connected SurrealDB version is not supported by the SDK.
Properties:
version(string) - The unsupported versionminimum(string) - Minimum supported version (inclusive)maximum(string) - Maximum supported version (exclusive)
Example:
Expression Errors
ExpressionError
Thrown when a SurrealQL expression fails to compile or execute.
Constructor: new ExpressionError(messageOrCause?: string | unknown)
When called with a string, it is used as the error message. When called with any other value (or no argument), the default message "Failed to parse invalid expression" is used and the argument is set as cause.
Example:
Event Errors
PublishError
Thrown when one or more event subscribers throw an error during publication.
Properties:
causes(unknown[]) - The errors thrown by subscribersmessage(string) - Summary message including the cause messages
Example:
Date and Time Errors
InvalidDateError
Thrown when a parsed date or datetime is invalid.
Constructor: new InvalidDateError(dateOrMessage: Date | string)
When called with a Date, the message is "The provided date is invalid: <date>". When called with a string, the string is used directly as the error message.
Example:
Feature Errors
UnsupportedFeatureError
Thrown when attempting to use a feature not supported by the configured engine.
Properties:
feature(Feature) - The unsupported feature
Example:
UnavailableFeatureError
Thrown when attempting to use a feature not available in the connected SurrealDB version.
Properties:
feature(Feature) - The unavailable featureversion(string) - The connected SurrealDB version
Example:
API Errors
UnsuccessfulApiError
Thrown when a user-defined API call fails.
Properties:
path(string) - The API path that was invokedmethod(string) - The HTTP method usedresponse(ApiResponse) - The error response from the API (includesbody?,headers?,status?)message(string) - Human-readable message (inherited from Error; includes path, method, and status)
Example:
Session Errors
InvalidSessionError
Thrown when attempting to use an invalid or disposed session.
Properties:
session(Session) - The invalid session identifier
Example:
Value Validation Errors
InvalidRecordIdError
Thrown when a RecordId or RecordIdRange is constructed with invalid parts.
Example:
InvalidDurationError
Thrown when a Duration string cannot be parsed or a duration operation is invalid.
Example:
InvalidDecimalError
Thrown when a Decimal operation fails (e.g. division by zero, invalid input).
Example:
InvalidTableError
Thrown when a Table or StringRecordId is constructed with an invalid value.
Example:
Error Handling Patterns
Basic Error Handling
Specific Error Handling
Error Recovery
Global Error Handler
Best Practices
1. Catch Specific Errors
Handle specific error types for better error recovery:
2. Use Type Guards
TypeScript type guards provide better type safety:
3. Log Error Details
Include error details in logs for debugging:
4. Clean Up on Error
Ensure resources are cleaned up even when errors occur:
See Also
Core Classes - Classes that may throw errors
Surreal - Connection methods that may throw errors
SurrealSession - Session methods that may throw errors
Source: errors.ts