sqlx_rxqlite/
error.rs

1use std::error::Error as StdError;
2use std::fmt::{self, Display, Formatter};
3use std::{borrow::Cow /*, str::from_utf8_unchecked*/};
4/*
5use libsqlite3_sys::{
6    sqlite3, sqlite3_errmsg, sqlite3_extended_errcode, SQLITE_CONSTRAINT_CHECK,
7    SQLITE_CONSTRAINT_FOREIGNKEY, SQLITE_CONSTRAINT_NOTNULL, SQLITE_CONSTRAINT_PRIMARYKEY,
8    SQLITE_CONSTRAINT_UNIQUE,
9};
10*/
11pub(crate) use sqlx_core::error::*;
12
13// Error Codes And Messages
14// https://www.sqlite.org/c3ref/errcode.html
15
16#[derive(Debug)]
17pub struct RXQLiteError {
18    pub inner: rxqlite_lite_common::RXQLiteError,
19}
20
21impl RXQLiteError {
22    /*
23    pub(crate) fn new(inner: Box<rxqlite_lite_common::RXQLiteError>) -> Self {
24        Self {
25            inner,
26        }
27    }
28    */
29}
30
31impl Display for RXQLiteError {
32    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
33        // We include the code as some produce ambiguous messages:
34        // SQLITE_BUSY: "database is locked"
35        // SQLITE_LOCKED: "database table is locked"
36        // Sadly there's no function to get the string label back from an error code.
37        write!(f, "{}", self.inner)
38    }
39}
40
41impl StdError for RXQLiteError {}
42
43impl DatabaseError for RXQLiteError {
44    #[inline]
45    fn message(&self) -> &str {
46        self.inner.description()
47    }
48
49    /// The extended result code.
50    #[inline]
51    fn code(&self) -> Option<Cow<'_, str>> {
52        //Some(format!("{}", self.code).into())
53        None
54    }
55
56    #[doc(hidden)]
57    fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
58        self
59    }
60
61    #[doc(hidden)]
62    fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
63        self
64    }
65
66    #[doc(hidden)]
67    fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
68        self
69    }
70
71    fn kind(&self) -> ErrorKind {
72        match self.inner {
73            /*
74            SQLITE_CONSTRAINT_UNIQUE | SQLITE_CONSTRAINT_PRIMARYKEY => ErrorKind::UniqueViolation,
75            SQLITE_CONSTRAINT_FOREIGNKEY => ErrorKind::ForeignKeyViolation,
76            SQLITE_CONSTRAINT_NOTNULL => ErrorKind::NotNullViolation,
77            SQLITE_CONSTRAINT_CHECK => ErrorKind::CheckViolation,
78            */
79            _ => ErrorKind::Other,
80        }
81    }
82}