forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_error.rs
More file actions
83 lines (72 loc) · 2.33 KB
/
Copy pathdatabase_error.rs
File metadata and controls
83 lines (72 loc) · 2.33 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
use diesel::result;
use std::convert::From;
use std::error::Error;
use std::path::PathBuf;
use std::{fmt, io};
use self::DatabaseError::*;
pub type DatabaseResult<T> = Result<T, DatabaseError>;
#[derive(Debug)]
pub enum DatabaseError {
ProjectRootNotFound(PathBuf),
DatabaseUrlMissing,
IoError(io::Error),
QueryError(result::Error),
ConnectionError(result::ConnectionError),
MigrationError(Box<dyn Error + Send + Sync + 'static>),
}
impl From<io::Error> for DatabaseError {
fn from(e: io::Error) -> Self {
IoError(e)
}
}
impl From<result::Error> for DatabaseError {
fn from(e: result::Error) -> Self {
QueryError(e)
}
}
impl From<result::ConnectionError> for DatabaseError {
fn from(e: result::ConnectionError) -> Self {
ConnectionError(e)
}
}
impl From<Box<dyn Error + Send + Sync + 'static>> for DatabaseError {
fn from(e: Box<dyn Error + Send + Sync + 'static>) -> Self {
MigrationError(e)
}
}
impl Error for DatabaseError {}
impl fmt::Display for DatabaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
ProjectRootNotFound(ref p) => {
write!(f, "Unable to find diesel.toml or Cargo.toml in {:?} or any parent directories.", p)
}
DatabaseUrlMissing => {
f.write_str("The --database-url argument must be passed, or the DATABASE_URL environment variable must be set.")
}
IoError(ref error) => f.write_str(&error
.source()
.map(ToString::to_string)
.unwrap_or_else(|| error.to_string())),
QueryError(ref error) => f.write_str(&error
.source()
.map(ToString::to_string)
.unwrap_or_else(|| error.to_string())),
ConnectionError(ref error) => f.write_str(&error
.source()
.map(ToString::to_string)
.unwrap_or_else(|| error.to_string())),
MigrationError(ref error) => {
write!(f, "Failed to run migrations: {}", error)
}
}
}
}
impl PartialEq for DatabaseError {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(&ProjectRootNotFound(_), &ProjectRootNotFound(_))
)
}
}