Crate sqlx_error

source ·
Expand description

sqlx-error

A wrapper around sqlx::Error to provide error path and additional context.

Usage

use sqlx_error::{sqlx_error, SqlxError};

#[derive(Debug, thiserror::Error)]
pub enum MyError {
    #[error(transparent)]
    Sqlx(#[from] SqlxError),
}

/// If you have a single sqlx query per function, the function path by itself could provide
/// enough context
fn foo() -> Result<(), MyError> {
    Err(sqlx::Error::RowNotFound).map_err(sqlx_error!())?;
    Ok(())
}

/// Or you can add more context
fn bar() -> Result<(), MyError> {
    Err(sqlx::Error::RowNotFound).map_err(sqlx_error!("more context"))?;
    Ok(())
}

assert_eq!(foo().unwrap_err().to_string(), "sqlx rust_out::foo, src/lib.rs:15:43");
assert_eq!(bar().unwrap_err().to_string(), "sqlx rust_out::bar, src/lib.rs:21:43, more context");

Macros

  • The macro adds error path and optional description tosqlx::Error.

Structs

  • Sqlx error wrapper to hold additional info

Type Aliases