#Using 'thiserror' to wrap a (Connection, rusqlite::Error)

6 messages · Page 1 of 1 (latest)

steep leaf
#

Hello, I have created an Enum to wrap all errors that the crate rusqlite has. So this includes rusqlite::Error and rusqlite::Types::FromSqlError

I'm doing this so I can have my functions that do database operations return this enum type as the error, and I can use the ? operator.

This is the relevant code I have:

use rusqlite::{Connection, Error, types::FromSqlError};
use thiserror::Error as terror;

#[derive(Debug, terror)]
pub enum DatabaseErrors {
    #[error("Rusqlite error: {0}")]
    RusqliteError(#[from] Error),
    #[error("FromSql error: {0}")]
    FromSqlError(#[from] FromSqlError),
    // #[error("Rusqlite Connection error: {0}")]
    // FromConnection(Connection, #[source] rusqlite::Error)
}

I receive this error when it tries to infer closing a database connection:

4  | pub fn add_obj(music_obj: PungeMusicObject) -> Result<(), DatabaseErrors> {
   |                                                -------------------------- expected `DatabaseErrors` because of this
...
12 |     conn.close()?;
   |                 ^ the trait `From<(Connection, rusqlite::Error)>` is not implemented for `DatabaseErrors`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following other types implement trait `From<T>`:
             <DatabaseErrors as From<FromSqlError>>
             <DatabaseErrors as From<rusqlite::Error>>
   = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, (Connection, rusqlite::Error)>>` for `Result<(), DatabaseErrors>`

I have tried to add the commented out part of the error enum above, but it does not change anything

Would appreciate some help with this!

crimson cipher
#

But a Result<T, (Connection, Error)>

#

Which means you need to call .map_err(|(_, err)| err) just before the ?

#

To throw away the connection

steep leaf
#

Ah, perfect. Makes sense as well. Don't know why i didn't think of that.