sqlx_core_guts/sqlite/
error.rs1use 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::from_utf8_unchecked};
6
7use libsqlite3_sys::{sqlite3, sqlite3_errmsg, sqlite3_extended_errcode};
8
9use crate::error::DatabaseError;
10
11#[derive(Debug)]
15pub struct SqliteError {
16 code: c_int,
17 message: String,
18}
19
20impl SqliteError {
21 pub(crate) fn new(handle: *mut sqlite3) -> Self {
22 let code: c_int = unsafe { sqlite3_extended_errcode(handle) };
24
25 let message = unsafe {
27 let msg = sqlite3_errmsg(handle);
28 debug_assert!(!msg.is_null());
29
30 from_utf8_unchecked(CStr::from_ptr(msg).to_bytes())
31 };
32
33 Self {
34 code,
35 message: message.to_owned(),
36 }
37 }
38}
39
40impl Display for SqliteError {
41 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42 f.pad(&self.message)
43 }
44}
45
46impl StdError for SqliteError {}
47
48impl DatabaseError for SqliteError {
49 #[inline]
51 fn code(&self) -> Option<Cow<'_, str>> {
52 Some(format!("{}", self.code).into())
53 }
54
55 #[inline]
56 fn message(&self) -> &str {
57 &self.message
58 }
59
60 #[doc(hidden)]
61 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
62 self
63 }
64
65 #[doc(hidden)]
66 fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
67 self
68 }
69
70 #[doc(hidden)]
71 fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
72 self
73 }
74}