-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
71 lines (57 loc) · 1.65 KB
/
Copy patherror.rs
File metadata and controls
71 lines (57 loc) · 1.65 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
use serde::Serialize;
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AppErrorCode {
NotFound,
Auth,
Forbidden,
Io,
Network,
Parse,
SecureStorage,
External,
}
#[derive(Debug, Clone, Serialize)]
pub struct AppError {
pub code: AppErrorCode,
pub message: String,
}
impl AppError {
pub fn new(code: AppErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::NotFound, message)
}
pub fn auth(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::Auth, message)
}
pub fn forbidden(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::Forbidden, message)
}
pub fn io(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::Io, message)
}
pub fn network(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::Network, message)
}
pub fn parse(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::Parse, message)
}
pub fn secure_storage(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::SecureStorage, message)
}
pub fn external(message: impl Into<String>) -> Self {
Self::new(AppErrorCode::External, message)
}
}
impl std::fmt::Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.message.fmt(f)
}
}
impl std::error::Error for AppError {}
pub type AppResult<T> = Result<T, AppError>;