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