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
67 lines (57 loc) · 1.79 KB
/
Copy pathdatabase_error.rs
File metadata and controls
67 lines (57 loc) · 1.79 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
use diesel::result;
use std::convert::From;
use std::{fmt, io};
use std::error::Error;
use self::DatabaseError::*;
pub type DatabaseResult<T> = Result<T, DatabaseError>;
#[derive(Debug)]
pub enum DatabaseError {
#[allow(dead_code)]
CargoTomlNotFound,
DatabaseUrlMissing,
IoError(io::Error),
QueryError(result::Error),
ConnectionError(result::ConnectionError),
}
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 Error for DatabaseError {
fn description(&self) -> &str {
match *self {
CargoTomlNotFound => "Unable to find Cargo.toml in this directory or any parent directories.",
DatabaseUrlMissing => "The --database-url argument must be passed, or the DATABASE_URL environment variable must be set.",
IoError(ref error) => error.cause().map(|e| e.description()).unwrap_or(error.description()),
QueryError(ref error) => error.cause().map(|e| e.description()).unwrap_or(error.description()),
ConnectionError(ref error) => error.cause().map(|e| e.description()).unwrap_or(error.description()),
}
}
}
impl fmt::Display for DatabaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.description().fmt(f)
}
}
impl PartialEq for DatabaseError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
&CargoTomlNotFound,
&CargoTomlNotFound,
) => true,
_ => false
}
}
}