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
use crate::core::context::Context;
use std::error::Error as StdError;
pub struct ThrusterError<C> {
pub context: C,
pub message: String,
pub cause: Option<Box<dyn StdError>>,
}
pub trait Error<C> {
fn build_context(self) -> C;
}
impl<C: Context> Error<C> for ThrusterError<C> {
fn build_context(self) -> C {
self.context
}
}
pub trait ErrorSet<C> {
fn parsing_error(context: C, error: &str) -> ThrusterError<C>;
fn generic_error(context: C) -> ThrusterError<C>;
fn unauthorized_error(context: C) -> ThrusterError<C>;
fn not_found_error(context: C) -> ThrusterError<C>;
fn server_error(context: C) -> ThrusterError<C>;
}
impl<C: Context> ErrorSet<C> for ThrusterError<C> {
fn parsing_error(mut context: C, error: &str) -> ThrusterError<C> {
context.status(400);
ThrusterError {
context,
message: format!("Failed to parse '{}'", error),
cause: None,
}
}
fn generic_error(mut context: C) -> ThrusterError<C> {
context.status(400);
ThrusterError {
context,
message: "Something didn't work!".to_string(),
cause: None,
}
}
fn unauthorized_error(mut context: C) -> ThrusterError<C> {
context.status(401);
ThrusterError {
context,
message: "Unauthorized".to_string(),
cause: None,
}
}
fn not_found_error(mut context: C) -> ThrusterError<C> {
context.status(404);
ThrusterError {
context,
message: "Not found".to_string(),
cause: None,
}
}
fn server_error(mut context: C) -> ThrusterError<C> {
context.status(500);
ThrusterError {
context,
message: "Server error".to_string(),
cause: None,
}
}
}
impl<C> std::fmt::Debug for ThrusterError<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ThrusterError")
.field("message", &self.message)
.finish()
}
}
impl<C: Clone> Clone for ThrusterError<C> {
fn clone(&self) -> Self {
ThrusterError {
context: self.context.clone(),
message: self.message.clone(),
cause: None,
}
}
}
impl<C: Clone + Default> From<Box<dyn std::error::Error>> for ThrusterError<C> {
fn from(e: Box<dyn std::error::Error>) -> Self {
ThrusterError {
context: C::default(),
message: e.to_string(),
cause: Some(e),
}
}
}