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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::prelude::*;
use std::error::Error as ErrorTrait;
use std::fmt::{self, Display, Formatter};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ErrorKind {
MutexPoisonError,
SystemTimeError(Duration),
SQLiteError,
NoneError,
Argon2ParsingError,
ClientSessionError,
Unspecified,
QueryError,
UnmanagedStateError,
FormValidationError,
UnauthenticatedClientError,
UnsafePasswordError,
Unauthorized,
RedisError,
JsonParsingError,
PostgresqlError,
IOError
}
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct Error {
pub message: String,
pub kind: ErrorKind,
}
impl ErrorTrait for Error {}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)
}
}
pub fn raise<T>(kind: ErrorKind, msg: &str) -> Result<T> {
Err(Error {
message: msg.into(),
kind,
})
}
pub trait SetErrorMessage {
type Ok;
fn msg(self, msg: &str) -> std::result::Result<Self::Ok, Error>;
}
impl<T, E: Into<Error> + ErrorTrait> SetErrorMessage for std::result::Result<T, E> {
type Ok = T;
fn msg(self, msg: &str) -> std::result::Result<T, Error> {
match self {
Ok(val) => Ok(val),
Err(error) => {
let mut error: Error = error.into();
error.message = msg.into();
Err(error)
}
}
}
}
use std::sync::PoisonError;
impl<T> From<PoisonError<T>> for Error {
fn from(error: PoisonError<T>) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::MutexPoisonError,
}
}
}
#[cfg(feature = "sqlite-db")]
impl From<rusqlite::Error> for Error {
fn from(error: rusqlite::Error) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::SQLiteError,
}
}
}
use std::time::SystemTimeError;
impl From<SystemTimeError> for Error {
fn from(error: SystemTimeError) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::SystemTimeError(error.duration()),
}
}
}
impl From<argon2::Error> for Error {
fn from(error: argon2::Error) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::Argon2ParsingError,
}
}
}
impl From<()> for Error {
fn from(_: ()) -> Error {
Error {
message: "".into(),
kind: ErrorKind::Unspecified,
}
}
}
impl From<&Error> for Error {
fn from(error: &Error) -> Error {
error.clone()
}
}
#[cfg(feature="redis-session")]
impl From<redis::RedisError> for Error {
fn from(error: redis::RedisError) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::RedisError,
}
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::JsonParsingError,
}
}
}
#[cfg(feature="postgres-db")]
impl From<tokio_postgres::Error> for Error {
fn from(error: tokio_postgres::Error) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::PostgresqlError,
}
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Error {
Error {
message: format!("{}", error),
kind: ErrorKind::IOError,
}
}
}