#error #error-context #sqlx #path #debugging #query #error-handling

sqlx-error

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

5 releases (3 breaking)

0.4.0 Sep 5, 2023
0.3.0 Sep 5, 2023
0.2.0 Jul 24, 2023
0.1.1 Jun 11, 2023
0.1.0 Jun 11, 2023

#2022 in Database interfaces

Download history 9/week @ 2024-01-15 28/week @ 2024-02-05 72/week @ 2024-02-19 9/week @ 2024-02-26 10/week @ 2024-03-04 8/week @ 2024-03-11 13/week @ 2024-03-18 9/week @ 2024-03-25 16/week @ 2024-04-01 31/week @ 2024-04-08 6/week @ 2024-04-15 8/week @ 2024-04-22

61 downloads per month

MIT license

6KB

License Crates.io Docs.rs

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");

Contributing

We appreciate all kinds of contributions, thank you!

Note on README

Most of the readme is automatically copied from the crate documentation by cargo-sync-readme. This way the readme is always in sync with the docs and examples are tested.

So if you find a part of the readme you'd like to change between <!-- cargo-sync-readme start --> and <!-- cargo-sync-readme end --> markers, don't edit README.md directly, but rather change the documentation on top of src/lib.rs and then synchronize the readme with:

cargo sync-readme

(make sure the cargo command is installed):

cargo install cargo-sync-readme

If you have rusty-hook installed the changes will apply automatically on commit.

License

This project is licensed under the MIT license.

Dependencies

~9.5MB
~195K SLoC