Skip to main content

walletkit_db/sqlite/
error.rs

1//! Error types for the safe `SQLite` wrapper.
2
3use std::fmt;
4
5/// Error code returned by `SQLite` operations.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct ErrorCode(pub i32);
8
9impl fmt::Display for ErrorCode {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        write!(f, "{}", self.0)
12    }
13}
14
15/// Error returned by database operations.
16#[derive(Debug, PartialEq, Eq)]
17pub struct Error {
18    /// `SQLite` result code.
19    pub code: ErrorCode,
20    /// Human-readable error message (from `sqlite3_errmsg` when available).
21    pub message: String,
22}
23
24impl Error {
25    /// Creates a new database error.
26    pub(crate) fn new(code: i32, message: impl Into<String>) -> Self {
27        Self {
28            code: ErrorCode(code),
29            message: message.into(),
30        }
31    }
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "sqlite error {}: {}", self.code, self.message)
37    }
38}
39
40impl std::error::Error for Error {}
41
42/// Result alias for [`Error`].
43pub type DbResult<T> = std::result::Result<T, Error>;