forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.rs
More file actions
95 lines (83 loc) · 2.67 KB
/
error.rs
File metadata and controls
95 lines (83 loc) · 2.67 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
use std::panic::Location;
use std::sync::Arc;
use r2d2_mysql::mysql::UrlError;
use super::driver::Driver;
use crate::located_error::{Located, LocatedError};
#[derive(thiserror::Error, Debug, Clone)]
pub enum Error {
#[error("The {driver} query unexpectedly returned nothing: {source}")]
QueryReturnedNoRows {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
driver: Driver,
},
#[error("The {driver} query was malformed: {source}")]
InvalidQuery {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
driver: Driver,
},
#[error("Unable to insert record into {driver} database, {location}")]
InsertFailed {
location: &'static Location<'static>,
driver: Driver,
},
#[error("Failed to remove record from {driver} database, error-code: {error_code}, {location}")]
DeleteFailed {
location: &'static Location<'static>,
error_code: usize,
driver: Driver,
},
#[error("Failed to connect to {driver} database: {source}")]
ConnectionError {
source: LocatedError<'static, UrlError>,
driver: Driver,
},
#[error("Failed to create r2d2 {driver} connection pool: {source}")]
ConnectionPool {
source: LocatedError<'static, r2d2::Error>,
driver: Driver,
},
}
impl From<r2d2_sqlite::rusqlite::Error> for Error {
#[track_caller]
fn from(err: r2d2_sqlite::rusqlite::Error) -> Self {
match err {
r2d2_sqlite::rusqlite::Error::QueryReturnedNoRows => Error::QueryReturnedNoRows {
source: (Arc::new(err) as Arc<dyn std::error::Error + Send + Sync>).into(),
driver: Driver::Sqlite3,
},
_ => Error::InvalidQuery {
source: (Arc::new(err) as Arc<dyn std::error::Error + Send + Sync>).into(),
driver: Driver::Sqlite3,
},
}
}
}
impl From<r2d2_mysql::mysql::Error> for Error {
#[track_caller]
fn from(err: r2d2_mysql::mysql::Error) -> Self {
let e: Arc<dyn std::error::Error + Send + Sync> = Arc::new(err);
Error::InvalidQuery {
source: e.into(),
driver: Driver::MySQL,
}
}
}
impl From<UrlError> for Error {
#[track_caller]
fn from(err: UrlError) -> Self {
Self::ConnectionError {
source: Located(err).into(),
driver: Driver::MySQL,
}
}
}
impl From<(r2d2::Error, Driver)> for Error {
#[track_caller]
fn from(e: (r2d2::Error, Driver)) -> Self {
let (err, driver) = e;
Self::ConnectionPool {
source: Located(err).into(),
driver,
}
}
}