pub enum BsqlError {
Pool(PoolError),
Query(QueryError),
Decode(DecodeError),
Connect(ConnectError),
}Expand description
The error type for all bsql operations.
§Variants
Pool— connection pool exhausted or misconfigured.Query— PostgreSQL rejected the query at runtime (triggers, RLS policies, constraint violations).Decode— a column value could not be converted to the expected Rust type.Connect— initial connection to PostgreSQL failed.
§Example
use bsql::{Pool, BsqlError};
let pool = Pool::connect("postgres://user:pass@localhost/mydb").await?;
// Match on error variants for fine-grained handling
let result = bsql::query!("INSERT INTO users (name) VALUES ($n: &str)")
.run(&pool).await;
match result {
Ok(affected) => println!("inserted {affected}"),
Err(e) if e.is_unique_violation() => println!("already exists"),
Err(e) => return Err(e),
}Variants§
Implementations§
Source§impl BsqlError
impl BsqlError
Sourcepub fn is_timeout(&self) -> bool
pub fn is_timeout(&self) -> bool
Whether this error is a PostgreSQL query cancellation / statement timeout (SQLSTATE 57014).
Sourcepub fn is_serialization_failure(&self) -> bool
pub fn is_serialization_failure(&self) -> bool
Whether this error is a serialization failure (SQLSTATE 40001).
When using SERIALIZABLE isolation, PostgreSQL may abort a transaction
with this code. The correct response is to retry the entire transaction.
Sourcepub fn is_unique_violation(&self) -> bool
pub fn is_unique_violation(&self) -> bool
Whether this error is a unique constraint violation (SQLSTATE 23505).
Common when inserting a row that would duplicate a unique index key. The error message typically includes which constraint was violated.
Sourcepub fn is_foreign_key_violation(&self) -> bool
pub fn is_foreign_key_violation(&self) -> bool
Whether this error is a foreign key violation (SQLSTATE 23503).
Raised when an INSERT or UPDATE references a row that does not exist in the referenced table, or a DELETE would leave dangling references.
Sourcepub fn is_not_null_violation(&self) -> bool
pub fn is_not_null_violation(&self) -> bool
Whether this error is a NOT NULL violation (SQLSTATE 23502).
Raised when an INSERT or UPDATE sets a NOT NULL column to NULL.
Sourcepub fn is_check_violation(&self) -> bool
pub fn is_check_violation(&self) -> bool
Whether this error is a check constraint violation (SQLSTATE 23514).
Sourcepub fn is_deadlock(&self) -> bool
pub fn is_deadlock(&self) -> bool
Whether this error is a deadlock (SQLSTATE 40P01).
PostgreSQL detected a deadlock between two or more transactions and chose this one as the victim. The correct response is to retry.
Sourcepub fn pg_code(&self) -> Option<&str>
pub fn pg_code(&self) -> Option<&str>
The PostgreSQL SQLSTATE code, if this is a query error with a code.
Returns None for non-query errors or query errors without a code
(e.g., I/O errors during query execution).
§Example
match err.pg_code() {
Some("23505") => println!("unique violation"),
Some("23503") => println!("foreign key violation"),
_ => {}
}Sourcepub fn from_driver_query(e: DriverError) -> Self
pub fn from_driver_query(e: DriverError) -> Self
Convert a DriverError that occurred during query execution.
Unlike the blanket From<DriverError> impl (which maps Io to Connect),
this maps Io errors to Query — because a network failure mid-query is
a query error, not a connection error.
Trait Implementations§
Source§impl Error for BsqlError
impl Error for BsqlError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()