Skip to main content

BsqlError

Enum BsqlError 

Source
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)")
    .execute(&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

Source

pub fn is_timeout(&self) -> bool

Whether this error is a PostgreSQL query cancellation / statement timeout (SQLSTATE 57014).

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_check_violation(&self) -> bool

Whether this error is a check constraint violation (SQLSTATE 23514).

Source

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.

Source

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"),
    _ => {}
}
Source

pub fn from_driver_query(e: DriverError) -> BsqlError

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 Debug for BsqlError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for BsqlError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Error for BsqlError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<DriverError> for BsqlError

Source§

fn from(e: DriverError) -> BsqlError

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.