Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error catalog

Generated rustdoc: https://docs.rs/babar/latest/babar/enum.Error.html

See also: Book Chapter 9 — Error handling.

Variants

Every babar::Error variant. There is no Error::kind() — match on the variant directly.

VariantShapeWhen it fires
IoIo(std::io::Error)TCP, TLS, or socket I/O failure (DNS, refused, reset, EOF).
ClosedClosed { sql: Option<String>, origin: Option<Origin> }The session was closed and the call lost its connection. sql and origin carry the in-flight statement.
ProtocolProtocol(String)The server sent something babar can’t make sense of (framing error, unexpected message).
AuthAuth(String)SCRAM rejected, password wrong, role can’t log in, no password configured.
UnsupportedAuthUnsupportedAuth(String)The server selected an auth method babar doesn’t speak (e.g. gss, sspi, or any code babar hasn’t implemented).
ServerServer { code, severity, message, detail, hint, position, sql, origin }An ErrorResponse from Postgres. code is the five-character SQLSTATE.
ConfigConfig(String)Bad client-side configuration (malformed TLS settings, bad timeouts, …).
CodecCodec(String)An Encoder / Decoder rejected the bytes — wrong column count, NULL where not expected, malformed wire bytes.
ColumnAlignmentColumnAlignment { expected, actual, sql, origin }A Decoder was expecting expected columns but RowDescription advertised actual.
SchemaMismatchSchemaMismatch { position, expected_oid, actual_oid, column_name, sql, origin }The Decoder’s declared OID at position doesn’t match the OID Postgres returned.
MigrationMigration(MigrationError)A migration step failed; the inner enum carries the migration-specific cause.

Closed, Server, ColumnAlignment, and SchemaMismatch carry an origin field that, with the sql! macro, points at the call site (file:line:col). Surfacing it in your logs almost always pays for itself the first time.

SQLSTATE patterns

The code field on Error::Server is a five-character SQLSTATE. This editorial section lists the codes most worth recognizing explicitly — it is guidance for application code, not a machine-extracted list. The full registry is in the Postgres docs (https://www.postgresql.org/docs/current/errcodes-appendix.html).

Constraint and concurrency

SQLSTATEClassCommon causeTypical reaction
23505unique_violationDuplicate key on insert/upsert.Map to a 409 in your service; consider INSERT ... ON CONFLICT.
23503foreign_key_violationInserting a row whose parent doesn’t exist.422 / validation error.
23502not_null_violationMissing required column.422 / validation error.
23514check_violationA CHECK constraint rejected the row.422 / validation error.
40001serialization_failureConflicting concurrent transactions at SERIALIZABLE.Retry with backoff.
40P01deadlock_detectedThe deadlock detector aborted your transaction.Retry; investigate the lock order.

Authentication and resource

SQLSTATEClassCommon cause
28P01invalid_passwordWrong password.
28000invalid_authorization_specificationRole can’t log in / pg_hba.conf rejected.
53300too_many_connectionsServer max_connections reached. Tune your pool.
57P03cannot_connect_nowServer in startup or recovery; retry shortly.

Schema

SQLSTATEClassCommon cause
42P01undefined_tableMissing table — typically a missing migration.
42703undefined_columnMissing column — schema drift.
42P07duplicate_tableA migration that already ran.

Choosing what to retry

A starting policy:

Variant / codeRetry?
Error::Io(_)Yes, with backoff. The connection is gone; the pool will reconnect.
Error::Server { code: "40001", .. }Yes — the whole transaction.
Error::Server { code: "40P01", .. }Yes — the whole transaction.
Error::Server { code: "57P03", .. }Yes, after a delay.
Error::Auth(_) / UnsupportedAuth(_)No. Surface to operator.
Error::Codec(_) / ColumnAlignment / SchemaMismatchNo. Fix the code.
Other Error::ServerNo by default; classify per SQLSTATE.

Next

For the codec inputs that produce Error::Codec / SchemaMismatch, see codecs.md. For the Config / PoolConfig knobs that produce Error::Config, see configuration.md.