forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patherror.rs
More file actions
107 lines (88 loc) 路 3.12 KB
/
Copy patherror.rs
File metadata and controls
107 lines (88 loc) 路 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::error::Error as StdError;
use std::ffi::CStr;
use std::fmt::{self, Display, Formatter};
use std::os::raw::c_int;
use std::{borrow::Cow, str::from_utf8_unchecked};
use libsqlite3_sys::{sqlite3, sqlite3_errmsg, sqlite3_error_offset, sqlite3_extended_errcode};
use crate::error::DatabaseError;
// Error Codes And Messages
// https://www.sqlite.org/c3ref/errcode.html
#[derive(Debug)]
pub struct SqliteError {
code: c_int,
offset: Option<usize>,
message: String,
}
impl SqliteError {
pub(crate) fn new(handle: *mut sqlite3) -> Self {
// returns the extended result code even when extended result codes are disabled
let code: c_int = unsafe { sqlite3_extended_errcode(handle) };
// sqlite3_error_offset: byte offset of the start of the token that caused the error, or -1 if unknown
let offset = usize::try_from(unsafe { sqlite3_error_offset(handle) }).ok();
// return English-language text that describes the error
let message = unsafe {
let msg = sqlite3_errmsg(handle);
debug_assert!(!msg.is_null());
from_utf8_unchecked(CStr::from_ptr(msg).to_bytes())
};
Self {
code,
offset,
message: message.to_owned(),
}
}
/// Indicates the byte offset of the start of the statement that failed withing the SQL string that was being executed.
pub(crate) fn with_statement_start_index(self, index: usize) -> Self {
Self {
offset: self.offset.map(|offset| offset + index),
..self
}
}
/// For errors during extension load, the error message is supplied via a separate pointer
pub(crate) fn extension(handle: *mut sqlite3, error_msg: &CStr) -> Self {
let mut err = Self::new(handle);
err.message = unsafe { from_utf8_unchecked(error_msg.to_bytes()).to_owned() };
err
}
}
impl Display for SqliteError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// We include the code as some produce ambiguous messages:
// SQLITE_BUSY: "database is locked"
// SQLITE_LOCKED: "database table is locked"
// Sadly there's no function to get the string label back from an error code.
write!(f, "(code: {}) {}", self.code, self.message)?;
if let Some(offset) = self.offset {
write!(f, " (at statement byte offset {})", offset)?;
};
Ok(())
}
}
impl StdError for SqliteError {}
impl DatabaseError for SqliteError {
/// The extended result code.
#[inline]
fn code(&self) -> Option<Cow<'_, str>> {
Some(format!("{}", self.code).into())
}
#[inline]
fn message(&self) -> &str {
&self.message
}
#[inline]
fn offset(&self) -> Option<usize> {
self.offset
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
self
}
}