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 is matchable directly in application code.

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 cannot interpret as a valid protocol exchange.
AuthAuth(String)SCRAM rejected, password wrong, role cannot log in, or no password was configured.
UnsupportedAuthUnsupportedAuth(String)The server selected an authentication method babar does not implement.
ServerServer { code, severity, message, detail, hint, position, sql, origin }A PostgreSQL ErrorResponse. code is the five-character SQLSTATE.
ConfigConfig(String)Client-side configuration is invalid.
CodecCodec(String)An encoder or decoder rejected the bytes or the row shape.
ColumnAlignmentColumnAlignment { expected, actual, sql, origin }The decoder expected expected columns but RowDescription advertised actual.
SchemaMismatchSchemaMismatch { position, expected_oid, actual_oid, column_name, sql, origin }The decoder’s declared OID at position differs from the OID PostgreSQL returned.
MigrationMigration(MigrationError)A migration step failed; the inner enum carries the migration-specific cause.

Closed, Server, ColumnAlignment, and SchemaMismatch carry an origin field. When a statement came from sql!, query!, command!, or a schema-scoped wrapper generated by schema!, that origin points back to the call site.

SQLSTATE patterns

The code field on Error::Server is a five-character SQLSTATE. This section is guidance for application code, not an exhaustive registry. The full list lives in the PostgreSQL documentation: 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 does not 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 cannot log in or pg_hba.conf rejected the connection.
53300too_many_connectionsServer max_connections reached.
57P03cannot_connect_nowServer is starting up or recovering.

Schema

SQLSTATEClassCommon cause
42P01undefined_tableMissing table, often a missing migration.
42703undefined_columnMissing column or schema drift.
42P07duplicate_tableSetup attempted to create an existing table.

Choosing what to retry

A practical starting policy:

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

Next

For codec shapes that can produce Error::Codec or SchemaMismatch, see codecs.md. For Config and PoolConfig settings that can produce Error::Config, see configuration.md.