forked from Trustless-OSS/Toss-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
159 lines (140 loc) · 4.05 KB
/
Copy patherror.rs
File metadata and controls
159 lines (140 loc) · 4.05 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
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
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::{json, Value};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("{message}")]
BadRequest { message: String },
#[error("{message}")]
Unauthorized { message: String },
#[error("{message}")]
Forbidden { message: String },
#[error("{message}")]
NotFound { message: String },
#[error("{message}")]
WebhookError {
message: String,
context: Option<Value>,
},
#[error("{message}")]
StellarError {
message: String,
context: Option<Value>,
},
#[error("{message}")]
GitHubError {
message: String,
context: Option<Value>,
},
#[error("{message}")]
DatabaseError {
message: String,
context: Option<Value>,
},
#[error("{message}")]
Internal { message: String },
#[error("{message}")]
EnvVarError { message: String },
}
impl AppError {
pub fn bad_request(message: impl Into<String>) -> Self {
Self::BadRequest {
message: message.into(),
}
}
pub fn unauthorized(message: impl Into<String>) -> Self {
Self::Unauthorized {
message: message.into(),
}
}
pub fn forbidden(message: impl Into<String>) -> Self {
Self::Forbidden {
message: message.into(),
}
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::NotFound {
message: message.into(),
}
}
pub fn webhook(message: impl Into<String>) -> Self {
Self::WebhookError {
message: message.into(),
context: None,
}
}
pub fn stellar(message: impl Into<String>) -> Self {
Self::StellarError {
message: message.into(),
context: None,
}
}
pub fn github(message: impl Into<String>) -> Self {
Self::GitHubError {
message: message.into(),
context: None,
}
}
pub fn database(message: impl Into<String>) -> Self {
Self::DatabaseError {
message: message.into(),
context: None,
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self::Internal {
message: message.into(),
}
}
pub fn env_var_error(name: impl Into<String>) -> Self {
Self::EnvVarError {
message: name.into(),
}
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let status = match &self {
Self::BadRequest { .. } => StatusCode::BAD_REQUEST,
Self::Unauthorized { .. } => StatusCode::UNAUTHORIZED,
Self::Forbidden { .. } => StatusCode::FORBIDDEN,
Self::NotFound { .. } => StatusCode::NOT_FOUND,
Self::WebhookError { .. }
| Self::StellarError { .. }
| Self::GitHubError { .. }
| Self::DatabaseError { .. }
| Self::Internal { .. }
| Self::EnvVarError { .. } => StatusCode::INTERNAL_SERVER_ERROR,
};
let message = self.to_string();
if status.is_server_error() {
tracing::error!(%status, error = %message, "request failed");
}
(status, Json(json!({ "error": message }))).into_response()
}
}
pub fn unauthorized_response() -> Response {
(
StatusCode::UNAUTHORIZED,
Json(json!({ "error": "Unauthorized" })),
)
.into_response()
}
/// Cheap clone of the Toasty pool handle (for `let mut db = require_db(&state.db)?;`).
pub fn require_db(db: &toasty::Db) -> Result<toasty::Db, AppError> {
Ok(db.clone())
}
pub fn map_db_err(error: impl std::fmt::Display) -> AppError {
AppError::database(error.to_string())
}
pub fn is_unique_violation(error: &impl std::fmt::Display) -> bool {
let message = error.to_string().to_lowercase();
message.contains("23505")
|| message.contains("unique")
|| message.contains("duplicate key")
|| message.contains("already exists")
}