The Go SDK returns errors that you inspect with errors.As and errors.Is. This page lists each error type, the kinds of a server error and the helpers that match them, and the sentinel errors. For patterns that check and retry these errors, see Error handling.
Packages:
github.com/surrealdb/surrealdb.go/v2, which re-exports the error types ofpkg/connectiongithub.com/surrealdb/surrealdb.go/v2/pkg/connection, which definesServerError,RPCErrorandHealthErrorgithub.com/surrealdb/surrealdb.go/v2/pkg/constants, which defines most sentinel errors
Error types
ServerError
An error that the server reports for a request, such as a sign-in, a CRUD function or a query that does not parse. Use errors.As to read it from any error the SDK returns.
type ServerError struct {
Code int
Message string
Kind string
Details any
Cause *ServerError
}| Field | Description |
|---|---|
Code | The JSON-RPC error code, such as -32000 |
Message | The message of the error |
Kind | The kind of the error, one of the Kind constants |
Details | Details for the kind, as { kind, details }, or nil |
Cause | The next error in the chain, or nil |
Methods:
.Error()returns the message, with the messages of the cause chain joined by": ".Unwrap()returnsCause, so thaterrors.Unwrap,errors.Isanderrors.Aswalk the chain
Available as: surrealdb.ServerError, an alias of connection.ServerError
_, err := surrealdb.Create[Person](ctx, db, models.NewRecordID("person", "tobie"), person)
var se *surrealdb.ServerError
if errors.As(err, &se) {
fmt.Println(se.Kind, se.Message)
}AlreadyExists Database record `person:tobie` already exists Kind constants
The surrealdb package re-exports the known values of ServerError.Kind:
| Constant | Value |
|---|---|
KindValidation | "Validation" |
KindConfiguration | "Configuration" |
KindThrown | "Thrown" |
KindQuery | "Query" |
KindSerialization | "Serialization" |
KindNotAllowed | "NotAllowed" |
KindNotFound | "NotFound" |
KindAlreadyExists | "AlreadyExists" |
KindConnection | "Connection" |
KindInternal | "Internal" |
Errors documents the kinds and their details for every SDK.
Is helpers
Each helper reports whether an error is, or wraps, a *ServerError that matches its entry in the table below. It returns false for nil and for other errors.
| Helper | Kind and detail |
|---|---|
IsNotFound(err) | NotFound |
IsNotAllowed(err) | NotAllowed |
IsInvalidAuth(err) | NotAllowed, Auth, InvalidAuth |
IsTokenExpired(err) | NotAllowed, Auth, TokenExpired |
IsScriptingBlocked(err) | NotAllowed, Scripting |
IsParseError(err) | Validation, Parse |
IsTimedOut(err) | Query, TimedOut |
IsCancelled(err) | Query, Cancelled |
IsNotExecuted(err) | Query, NotExecuted |
IsTransactionConflict(err) | Query, TransactionConflict, which is safe to retry |
IsDeserialization(err) | Serialization, Deserialization |
IsLiveQueryNotSupported(err) | Configuration, LiveQueryNotSupported |
if surrealdb.IsNotFound(err) {
fmt.Println("no such table")
} QueryError
The error of one failed statement of a query. It is the Error field of a QueryResult, and the Err field of the Done frame of a StreamFrame. The error that Query returns joins the QueryError of each failed statement.
type QueryError struct {
Message string
}Methods:
.Error()returns the message.Is(target)returnstruewhentargetis a*QueryError, soerrors.Is(err, &surrealdb.QueryError{})reports whethererrholds one
A QueryError holds the message only, so the Is helpers do not match it.
results, err := surrealdb.Query[any](ctx, db, "RETURN 1; THROW 'Not allowed';", nil)
if errors.Is(err, &surrealdb.QueryError{}) {
for _, qr := range *results {
if qr.Error != nil {
fmt.Println(qr.Error.Message)
}
}
}An error occurred: Not allowed RPCError
The JSON-RPC error of a failed request, with Code and Message fields. It unwraps to a *ServerError, which has more information. Use errors.As with a *ServerError instead.
Available as: surrealdb.RPCError, an alias of connection.RPCError. It is deprecated.
UnsupportedVersionError
The client returns this error when it connects to a server whose version is outside the range that the SDK supports. It does this only with WithVersionCheck(true).
type UnsupportedVersionError struct {
Version string
Minimum string
Maximum string
}| Field | Description |
|---|---|
Version | The version the server reported |
Minimum | The oldest version supported, MinimumServerVersion |
Maximum | The first version not supported, MaximumServerVersion |
HealthError
db.Health() returns this error when the server answers its health check with a status other than 2xx. The server does this, for example, when it cannot reach its datastore.
type HealthError struct {
StatusCode int
Status string
Body string
}| Field | Description |
|---|---|
StatusCode | The HTTP status code, such as 503 |
Status | The HTTP status, such as 503 Service Unavailable |
Body | The start of the body, at most 4 KiB, with surrounding white space removed |
Available as: surrealdb.HealthError, an alias of connection.HealthError
ImportError
db.Import() returns this error when statements of the document fail. The server does not roll back the statements that succeed.
type ImportError struct {
Failures []error
}Failures holds the error of each failed statement, in order. Each error is an *RPCError that unwraps to a *ServerError.
Methods:
.Error()describes the first failure and the number of failures.Unwrap()returnsFailures, soerrors.Isanderrors.Aslook through each of them
Sentinel errors
Check these with errors.Is, because the SDK wraps some of them.
In pkg/constants
| Error | Returned when |
|---|---|
ErrSessionsNotSupported | You call Attach, Fork or Sessions on an HTTP connection |
ErrTransactionsNotSupported | You call Begin on an HTTP connection |
ErrSessionClosed | You use a session after .Detach() |
ErrTransactionClosed | You use a transaction after .Commit() or .Cancel() |
ErrForkTokenExpired | You call Fork after the access token of the session has expired |
ErrForkUntracked | You call Fork on a session that this client did not attach |
ErrNeedsCBOR2 | The SDK tries to encode a value with no plain CBOR form, such as a models.Regex, for plain CBOR |
ErrMethodNotAvailable | A method is not available on the connection |
ErrNoNamespaceOrDB is deprecated. The HTTP connection no longer needs a namespace and database for every request, and does not return it.
In the surrealdb package
| Error | Returned when |
|---|---|
ErrClientClosed | You use the client after .Close(), or after it gave up reconnecting |
ErrLiveQueryEnded | The client ended a live query, instead of restarting it, when it reconnected |
ErrStreamProtocol | The frames of a streamed query break the streaming protocol |
In pkg/connection
| Error | Returned when |
|---|---|
ErrConnectionClosed | The client lost the connection while a request waited for its answer |
A request that times out returns an error that wraps context.DeadlineExceeded.
import "github.com/surrealdb/surrealdb.go/v2/pkg/constants"
session, err := db.Attach(ctx)
if errors.Is(err, constants.ErrSessionsNotSupported) {
log.Fatal("sessions need a WebSocket connection")
}See also
Error handling for patterns that check errors, and for retry guidance
Types reference for
QueryResultandStreamFrameDB reference for the functions that return these errors
Errors for the error kinds that every SDK shares