-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patherror.rs
More file actions
74 lines (64 loc) · 2.09 KB
/
error.rs
File metadata and controls
74 lines (64 loc) · 2.09 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
use alloc::{
format,
string::{String, ToString},
};
use core::error::Error;
use sqlite_nostd::{context, sqlite3, Connection, Context, ResultCode};
#[derive(Debug)]
pub struct SQLiteError(pub ResultCode, pub Option<String>);
impl core::fmt::Display for SQLiteError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}
impl SQLiteError {
pub fn apply_to_ctx(self, description: &str, ctx: *mut context) {
let SQLiteError(code, message) = self;
if message.is_some() {
ctx.result_error(&format!("{:} {:}", description, message.unwrap()));
} else {
let error = ctx.db_handle().errmsg().unwrap();
if error == "not an error" {
ctx.result_error(&format!("{:}", description));
} else {
ctx.result_error(&format!("{:} {:}", description, error));
}
}
ctx.result_error_code(code);
}
}
impl Error for SQLiteError {}
pub trait PSResult<T> {
fn into_db_result(self, db: *mut sqlite3) -> Result<T, SQLiteError>;
}
impl<T> PSResult<T> for Result<T, ResultCode> {
fn into_db_result(self, db: *mut sqlite3) -> Result<T, SQLiteError> {
if let Err(code) = self {
let message = db.errmsg().unwrap_or(String::from("Conversion error"));
if message == "not an error" {
Err(SQLiteError(code, None))
} else {
Err(SQLiteError(code, Some(message)))
}
} else if let Ok(r) = self {
Ok(r)
} else {
Err(SQLiteError(ResultCode::ABORT, None))
}
}
}
impl From<ResultCode> for SQLiteError {
fn from(value: ResultCode) -> Self {
SQLiteError(value, None)
}
}
impl From<serde_json::Error> for SQLiteError {
fn from(value: serde_json::Error) -> Self {
SQLiteError(ResultCode::ABORT, Some(value.to_string()))
}
}
impl From<core::fmt::Error> for SQLiteError {
fn from(value: core::fmt::Error) -> Self {
SQLiteError(ResultCode::INTERNAL, Some(format!("{}", value)))
}
}