Skip to content

SQL drivers for JavaScript

SQL drivers errors

Every error the native API throws extends SurrealSqlError, and every class is exported.

ErrorWhen
StatementErrorA statement failed. It has sqlstate, errno, severity, position and the partial results.
CapabilityDeniedErrorThe instance denies the method or the dialect route, for example with --deny-http postgres or --deny-rpc pgsql.
AuthErrorSign-in or token failure.
RpcErrorThe call itself failed. It has code, kind and details. A "Method not found" reply means the instance may not serve the SQL RPC methods.
ConnectionError, ConnectionLostErrorNetwork failure. ConnectionLostError has inTransaction.
AbortError, TimeoutErrorThe caller aborted, or the timeout elapsed.
UnknownResponseErrorA reply that is not an RPC reply, such as a proxy page. It keeps the status and the first 4096 characters of the body.
UnsupportedOnTransportErrorbegin or live on a transport pinned to HTTP.
import { connect, StatementError } from "@surrealdb/pg";

try {
    await sql`SELECT * FROM missing`;
} catch (error) {
    if (error instanceof StatementError && error.sqlstate === "42P01") {
        // the relation does not exist
    }
}

The compatibility layers convert StatementError to the shape their ecosystem expects.

LayerErrorFields
node-postgres (@surrealdb/pg)DatabaseErrorcode (the SQLSTATE), severity, message, position
mysql2 (@surrealdb/mysql)MysqlErrorcode, errno, sqlState, sqlMessage, sql
Neon (@surrealdb/pg/neon)NeonDbErrorThe node-postgres fields, and sourceError for failures before the instance answers
PlanetScale (@surrealdb/mysql/planetscale)DatabaseErrorbody.message, body.code, status, errno, sqlState

The mysql2 layer names the common error numbers (1045, 1048, 1049, 1050, 1051, 1054, 1062, 1064, 1146, 1205, 1213, 1216, 1217, 1317, 1451 and 1452), for example ER_DUP_ENTRY for 1062. Any other number gets the code ER_UNKNOWN, with the number in errno.

The drivers never retry a write. A call that fails authentication in token mode is retried once with a fresh sign-in, and nothing else is retried.

Was this page helpful?