Version 2 of the PHP SDK throws typed exceptions for different failures. They all extend SurrealException, which in turn extends PHP's RuntimeException. This lets you catch SDK errors broadly or target a specific failure with an instanceof check or a catch type.
Common exceptions
All exceptions live in the SurrealDB\SDK\Exceptions namespace.
| Exception | Thrown when |
|---|---|
SurrealException | Base class for every SDK error |
ConnectionUnavailableException | An operation runs without an active connection |
HttpConnectionException | An HTTP request fails with a non-success status |
AuthenticationException | Sign in, sign up, or token renewal fails |
MissingNamespaceDatabaseException | An operation needs a namespace or database that is not selected |
ServerException | The server reports an error (base for query errors) |
QueryException | A statement fails to execute |
UnsupportedVersionException | The server version is outside the supported range |
UnsupportedFeatureException | The engine does not support a requested feature |
UnavailableFeatureException | The server version does not support a requested feature |
Catching exceptions
Catch a specific type for fine-grained handling, or SurrealException to handle any SDK error.
Connection and HTTP errors
A ConnectionUnavailableException is thrown when you run an operation without a connection. Over HTTP, a failed request throws HttpConnectionException, which exposes the status code, status text, and response body.
Query errors
When a statement fails, the SDK throws a ServerException (or a subclass such as QueryException). The exception carries the server's kind, message, and any details.
Version mismatches
The SDK checks the server version on connect. If it is outside the supported range, it throws UnsupportedVersionException with the reported version and the supported bounds. Disable the check with the versionCheck option on connect().
Listening to connection errors
Errors that happen outside a direct method call, such as a failed reconnection, are delivered through the error event rather than thrown. Subscribe with subscribe().
Learn more
Surreal API reference for the methods that raise these errors
Connecting to SurrealDB for version and reconnection settings
Authentication for handling sign-in failures