ORM-01: Add Error and a Result alias #1

Closed
opened 2026-10-13 12:49:38 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: none

Context

Every fallible call returns rusqlite::Result today. Migrations (checksum drift, invalid lists) and relations (NotPersisted) need errors that are not SQLite errors, so the crate needs its own error type before anything else is built on top of it.

Tasks

  • New src/error.rs with a #[non_exhaustive] pub enum Error, starting with Sqlite(rusqlite::Error), NotConnected, AlreadyConnected, NotPersisted, NotFound. Later issues add variants.
  • pub type Result<T> = std::result::Result<T, Error>.
  • Hand-written Display, std::error::Error (with source()) and From<rusqlite::Error>. No new dependency.
  • Re-export Error and Result from lib.rs.
  • Switch Model::insert, Query::all and Query::first to the new Result. Model::from_row keeps returning rusqlite::Result.

Example

// src/error.rs
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    Sqlite(rusqlite::Error),
    NotConnected,
    AlreadyConnected,
    NotPersisted,
    NotFound,
}

pub type Result<T> = std::result::Result<T, Error>;

impl From<rusqlite::Error> for Error {
    fn from(e: rusqlite::Error) -> Self {
        Error::Sqlite(e)
    }
}

? keeps working on rusqlite calls inside functions that return the new Result:

pub fn first(self, conn: &Connection) -> Result<Option<M>> {
    Ok(self.limit(1).all(conn)?.into_iter().next()) // rusqlite::Error -> Error via From
}
#[test]
fn source_is_the_rusqlite_error() {
    use std::error::Error as _;
    let err = tessera_orm::Error::from(rusqlite::Error::QueryReturnedNoRows);
    assert!(err.source().is_some());
}

Acceptance criteria

  • Existing tests compile with only import or ? changes.
  • Error::source() returns the wrapped rusqlite error for Error::Sqlite.
  • cargo test and cargo clippy are green.
**Depends on:** none ## Context Every fallible call returns `rusqlite::Result` today. Migrations (checksum drift, invalid lists) and relations (`NotPersisted`) need errors that are not SQLite errors, so the crate needs its own error type before anything else is built on top of it. ## Tasks - [x] New `src/error.rs` with a `#[non_exhaustive] pub enum Error`, starting with `Sqlite(rusqlite::Error)`, `NotConnected`, `AlreadyConnected`, `NotPersisted`, `NotFound`. Later issues add variants. - [x] `pub type Result<T> = std::result::Result<T, Error>`. - [x] Hand-written `Display`, `std::error::Error` (with `source()`) and `From<rusqlite::Error>`. No new dependency. - [x] Re-export `Error` and `Result` from `lib.rs`. - [x] Switch `Model::insert`, `Query::all` and `Query::first` to the new `Result`. `Model::from_row` keeps returning `rusqlite::Result`. ## Example ```rust // src/error.rs #[derive(Debug)] #[non_exhaustive] pub enum Error { Sqlite(rusqlite::Error), NotConnected, AlreadyConnected, NotPersisted, NotFound, } pub type Result<T> = std::result::Result<T, Error>; impl From<rusqlite::Error> for Error { fn from(e: rusqlite::Error) -> Self { Error::Sqlite(e) } } ``` `?` keeps working on rusqlite calls inside functions that return the new `Result`: ```rust pub fn first(self, conn: &Connection) -> Result<Option<M>> { Ok(self.limit(1).all(conn)?.into_iter().next()) // rusqlite::Error -> Error via From } ``` ```rust #[test] fn source_is_the_rusqlite_error() { use std::error::Error as _; let err = tessera_orm::Error::from(rusqlite::Error::QueryReturnedNoRows); assert!(err.source().is_some()); } ``` ## Acceptance criteria - [x] Existing tests compile with only import or `?` changes. - [x] `Error::source()` returns the wrapped rusqlite error for `Error::Sqlite`. - [x] `cargo test` and `cargo clippy` are green.
Sign in to join this conversation.
No description provided.