The Go SDK returns ordinary Go errors, which you inspect with errors.As and errors.Is. A failure belongs to one of three groups:
An error that the server reports for a request
The error of one statement in a query
A failure in the client or the network
This page shows how to tell the groups apart and how to match on the kind of a server error. It also shows which errors are safe to retry.
The Errors page documents the error kinds, wire codes and structured shape behind these errors once, for every SDK and protocol.
API references
Error | Description |
|---|---|
ServerError | An error from the server, with a kind, details and a cause chain |
QueryError | The error of one failed statement in a query |
IsNotFound, IsNotAllowedand others | Report whether an error is a server error of a given kind |
UnsupportedVersionError | The server version is outside the range the SDK supports |
HealthError | The server answered the health check with an unsuccessful status |
ImportError | Statements of an imported document failed |
Sentinel errors | Fixed errors, such as ErrSessionClosedand ErrClientClosed |
Where an error surfaces
The SDK reports each kind of failure in its own way:
| Failure | How it surfaces |
|---|---|
| The server refuses a request, such as a sign-in, a CRUD function, or a whole query that does not parse | The returned error is an *RPCError that unwraps to a *ServerError |
| One statement of a query fails | The statement's QueryResult.Error is a *QueryError, and the returned error joins them |
| The connection is lost, or a request times out | The returned error wraps connection.ErrConnectionClosed or context.DeadlineExceeded |
| The result does not fit the type parameter | The returned error starts with Send: error unmarshaling result |
| A call is not possible on this client, such as a session over HTTP | A sentinel error, such as constants.ErrSessionsNotSupported |
Handling server errors
Use errors.As to read the ServerError of a failed request. It holds the Kind of the error, a Message, kind-specific Details, and an optional Cause:
_, err := surrealdb.Create[Person](ctx, db, models.NewRecordID("person", "tobie"), person)
if err != nil {
var se *surrealdb.ServerError
if errors.As(err, &se) {
fmt.Println(se.Kind, se.Details)
}
}AlreadyExists map[details:map[id:person:tobie] kind:Record]Cause is the next error in the chain, and is itself a *ServerError. .Error() joins the messages of the chain with ": ". To go through the chain one error at a time, use errors.Unwrap.
Matching on the kind
Compare Kind with the Kind constants, such as surrealdb.KindAlreadyExists. You can also use one of the Is helpers, which find the *ServerError for you:
_, err := surrealdb.Select[[]Person](ctx, db, models.Table("person"))
switch {
case surrealdb.IsNotFound(err):
fmt.Println("the table does not exist yet")
case surrealdb.IsNotAllowed(err):
fmt.Println("not allowed to read the table")
case err != nil:
log.Fatal(err)
}| Helper | Matches |
|---|---|
IsNotFound | A table, record, namespace, database, session or transaction that does not exist |
IsNotAllowed | Any NotAllowed error, such as a permission or an authentication failure |
IsInvalidAuth | Credentials that the server refused |
IsTokenExpired | An access token that has expired |
IsScriptingBlocked | A script that the server's capabilities do not allow |
IsParseError | A query that does not parse |
IsTimedOut | A query that passed its TIMEOUT |
IsCancelled | A query that was cancelled |
IsNotExecuted | A statement that never ran, usually because an earlier statement in its transaction failed |
IsTransactionConflict | A transaction that conflicted with another one, which is safe to retry |
IsDeserialization | A value that the server could not decode |
IsLiveQueryNotSupported | A live query on a connection or server that cannot run one |
Match on the kind rather than the message. The text of a message can change between SurrealDB versions. Also, from version 2 of the SDK, the CRUD functions run as queries, which changed some of their messages.
Handling statement errors
Query returns a result for each statement, and a statement can fail while the others succeed. The QueryResult.Error of the failed statement is then a *QueryError:
results, err := surrealdb.Query[any](ctx, db,
"CREATE person:tobie; CREATE person:tobie;",
nil,
)
for i, qr := range *results {
if qr.Error != nil {
fmt.Printf("statement %d failed: %s\n", i, qr.Error.Message)
}
}statement 1 failed: Database record `person:tobie` already existsThe returned err joins the *QueryError of every failed statement. errors.Is(err, &surrealdb.QueryError{}) reports whether err holds one. Use it to tell a statement failure apart from a transport or decoding failure:
if errors.Is(err, &surrealdb.QueryError{}) {
fmt.Println("one or more statements failed")
}A QueryError holds the message only, so the Is helpers do not match it. To get the kind of a failure, run the operation with a CRUD function, which returns a *ServerError. You can also check the message of the statement.
Errors from connecting
Connecting can fail before the client sends any request:
| Error | Cause |
|---|---|
invalid connection url | The URL has no scheme, or a scheme other than ws, wss, http or https |
embedded database not enabled | The URL uses mem://, memory or surrealkv://. See Embedding. |
*UnsupportedVersionError | With WithVersionCheck(true), the server version is outside the supported range |
A network error, such as connection refused | The server cannot be reached |
.Health() returns a *HealthError when the server answers its health check with a status other than 2xx:
var he *surrealdb.HealthError
if err := db.Health(ctx); errors.As(err, &he) {
log.Printf("server unhealthy: %d %s", he.StatusCode, he.Body)
}Errors from an import
DB.Import keeps every statement of a document that succeeds, even when other statements fail. When some fail, it returns an *ImportError, which lists the error of each failed statement:
err := db.Import(ctx, file)
var ie *surrealdb.ImportError
if errors.As(err, &ie) {
for _, failure := range ie.Failures {
log.Println(failure)
}
}Each failure unwraps to a *ServerError, so the Is helpers work on it. See Import and export.
Checking sentinel errors
The SDK returns fixed errors for conditions that do not come from the server. Check them with errors.Is:
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")
}| Error | Returned when |
|---|---|
constants.ErrSessionsNotSupported | Attach, Fork or Sessions is called on an HTTP connection |
constants.ErrTransactionsNotSupported | Begin is called on an HTTP connection |
constants.ErrSessionClosed | A session is used after .Detach() |
constants.ErrTransactionClosed | A transaction is used after .Commit() or .Cancel() |
constants.ErrForkTokenExpired | Fork is called after the session's access token has expired |
constants.ErrForkUntracked | Fork is called on a session that this client did not attach |
constants.ErrNeedsCBOR2 | A value with no plain CBOR form, such as a models.Regex, is sent to a server before SurrealDB 4.0 |
surrealdb.ErrClientClosed | The client is used after .Close() |
surrealdb.ErrLiveQueryEnded | A live query was ended, instead of restarted, when the client reconnected |
surrealdb.ErrStreamProtocol | The frames of a streamed query break the streaming protocol |
connection.ErrConnectionClosed | The connection was lost while a request waited for its answer |
The SDK wraps the errors in constants in some cases, so use errors.Is rather than ==.
Deciding whether to retry
Retry only a request that failed for a reason that does not depend on the request itself:
| Error | Retry | Reason |
|---|---|---|
IsTransactionConflict | Yes | Another transaction changed the same data. Running it again usually succeeds. |
context.DeadlineExceeded | Yes, with care | The request timed out, and may or may not have run. Retry only work that is safe to repeat. |
connection.ErrConnectionClosed | Yes, with care | The connection was lost while the request was in flight, so it may or may not have run. A reconnecting client sends later requests on the new connection. |
QueryError | No | The statement itself failed, for example because of its data or a THROW. |
Another ServerError kind, such as NotAllowed or AlreadyExists | No | The request is refused, and sending it again gives the same answer. |
| A decoding error | No | The type parameter does not match the result. |
SurrealDB 3.x servers
SurrealDB 3.x reports some errors with less precise kinds than 4.0. For example, a value that does not match the type of a field gives an Internal error with code -32000 on 3.x. On 4.0, it gives a Validation error with code -32600. Code that has to work with both versions should not depend on Kind or Details having the 4.0 values.
Learn more
Errors API reference for every error type and sentinel
Executing queries for statement results
Reliable connections for requests during a reconnect
Errors for the error kinds that every SDK shares