sqlx_sqlite/
error.rs

1use std::error::Error as StdError;
2use std::ffi::CStr;
3use std::fmt::{self, Display, Formatter};
4use std::os::raw::c_int;
5use std::{borrow::Cow, str};
6
7use libsqlite3_sys::{
8    sqlite3, sqlite3_errmsg, sqlite3_errstr, sqlite3_extended_errcode, SQLITE_CONSTRAINT_CHECK,
9    SQLITE_CONSTRAINT_FOREIGNKEY, SQLITE_CONSTRAINT_NOTNULL, SQLITE_CONSTRAINT_PRIMARYKEY,
10    SQLITE_CONSTRAINT_UNIQUE, SQLITE_ERROR,
11};
12
13pub(crate) use sqlx_core::error::*;
14
15// Error Codes And Messages
16// https://www.sqlite.org/c3ref/errcode.html
17
18#[derive(Debug)]
19pub struct SqliteError {
20    code: c_int,
21    message: Cow<'static, str>,
22}
23
24impl SqliteError {
25    pub(crate) unsafe fn new(handle: *mut sqlite3) -> Self {
26        Self::try_new(handle).expect("There should be an error")
27    }
28
29    pub(crate) unsafe fn try_new(handle: *mut sqlite3) -> Option<Self> {
30        // returns the extended result code even when extended result codes are disabled
31        let code: c_int = unsafe { sqlite3_extended_errcode(handle) };
32
33        if code == 0 {
34            return None;
35        }
36
37        // return English-language text that describes the error
38        let message = unsafe {
39            let msg = sqlite3_errmsg(handle);
40            debug_assert!(!msg.is_null());
41
42            str::from_utf8_unchecked(CStr::from_ptr(msg).to_bytes()).to_owned()
43        };
44
45        Some(Self {
46            code,
47            message: message.into(),
48        })
49    }
50
51    /// For errors during extension load, the error message is supplied via a separate pointer
52    pub(crate) fn with_message(mut self, error_msg: String) -> Self {
53        self.message = error_msg.into();
54        self
55    }
56
57    pub(crate) fn from_code(code: c_int) -> Self {
58        let message = unsafe {
59            let errstr = sqlite3_errstr(code);
60
61            if !errstr.is_null() {
62                // SAFETY: `errstr` is guaranteed to be UTF-8
63                // The lifetime of the string is "internally managed";
64                // the implementation just selects from an array of static strings.
65                // We copy to an owned buffer in case `libsqlite3` is dynamically loaded somehow.
66                Cow::Owned(str::from_utf8_unchecked(CStr::from_ptr(errstr).to_bytes()).into())
67            } else {
68                Cow::Borrowed("<error message unavailable>")
69            }
70        };
71
72        SqliteError { code, message }
73    }
74
75    pub(crate) fn generic(message: impl Into<Cow<'static, str>>) -> Self {
76        Self {
77            code: SQLITE_ERROR,
78            message: message.into(),
79        }
80    }
81}
82
83impl Display for SqliteError {
84    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
85        // We include the code as some produce ambiguous messages:
86        // SQLITE_BUSY: "database is locked"
87        // SQLITE_LOCKED: "database table is locked"
88        // Sadly there's no function to get the string label back from an error code.
89        write!(f, "(code: {}) {}", self.code, self.message)
90    }
91}
92
93impl StdError for SqliteError {}
94
95impl DatabaseError for SqliteError {
96    #[inline]
97    fn message(&self) -> &str {
98        &self.message
99    }
100
101    /// The extended result code.
102    #[inline]
103    fn code(&self) -> Option<Cow<'_, str>> {
104        Some(format!("{}", self.code).into())
105    }
106
107    #[doc(hidden)]
108    fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
109        self
110    }
111
112    #[doc(hidden)]
113    fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
114        self
115    }
116
117    #[doc(hidden)]
118    fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
119        self
120    }
121
122    fn kind(&self) -> ErrorKind {
123        match self.code {
124            SQLITE_CONSTRAINT_UNIQUE | SQLITE_CONSTRAINT_PRIMARYKEY => ErrorKind::UniqueViolation,
125            SQLITE_CONSTRAINT_FOREIGNKEY => ErrorKind::ForeignKeyViolation,
126            SQLITE_CONSTRAINT_NOTNULL => ErrorKind::NotNullViolation,
127            SQLITE_CONSTRAINT_CHECK => ErrorKind::CheckViolation,
128            _ => ErrorKind::Other,
129        }
130    }
131}