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