Error handling
All errors raised by the Python SDK extend SurrealError, so you can catch every SDK error with a single except clause. Server-originated errors use the ServerError subtree with structured kinds, details, and cause chains. SDK-side errors cover connection, parsing, and feature support failures.
See the Errors reference for the complete error hierarchy and all available properties.
API References
| Error class | Description |
|---|---|
SurrealError | Base class for all SDK errors |
ServerError | Structured server error with kind, details, and cause |
NotAllowedError | Thrown when permission is denied |
NotFoundError | Thrown when a resource is not found |
ConnectionUnavailableError | Thrown when no connection is active |
UnsupportedFeatureError | Thrown for features not supported by the connection type |
Catching all SDK errors
The simplest way to handle errors is to catch SurrealError, which is the base class for every exception the SDK raises.
This pattern is useful at the top level of your application where you want to ensure no SDK error goes unhandled.
Handling server errors
Server errors carry structured information beyond the error message. A ServerError has a .kind string, an optional .details dictionary, and an optional .server_cause linking to the underlying error in the chain.
You can check whether an error is a ServerError and then inspect its kind using the constants defined on ErrorKind.
The ErrorKind constants include VALIDATION, CONFIGURATION, THROWN, QUERY, SERIALIZATION, NOT_ALLOWED, NOT_FOUND, ALREADY_EXISTS, CONNECTION, and INTERNAL.
Inspecting the error cause chain
Server errors can form a chain where one error caused another. The .has_kind() method checks whether this error or any error in its cause chain matches a given kind. The .find_cause() method returns the first matching error in the chain.
These methods are especially useful when a high-level error wraps a more specific cause, such as a query error that was ultimately caused by a permission denial.
Catching specific error types
For fine-grained control, catch the specific error subclass you need. The SDK maps server error kinds to dedicated Python classes such as ValidationError, NotAllowedError, and NotFoundError.
You can also catch NotFoundError to handle missing resources.
Handling SDK-side errors
Some errors originate from the SDK itself rather than the server. These cover situations like missing connections and unsupported features.
A ConnectionUnavailableError is raised when you try to perform an operation before establishing a connection.
An UnsupportedFeatureError is raised when you attempt to use a feature that requires a specific connection type. For example, sessions and transactions require a WebSocket connection.
An UnsupportedEngineError is raised when the URL scheme is not recognized.
Learn more
Errors reference for complete error hierarchy
ErrorKind constants for error kind matching
Authentication for auth-related error patterns
Connecting to SurrealDB for connection error patterns