tangled
alpha
login
or
join now
ericwood.org
/
photos-site
1
fork
atom
A little app to serve my photography from my personal website
1
fork
atom
overview
issues
pulls
pipelines
show template errors
ericwood.org
2 months ago
82f3b67f
b4e2884f
0/0
Waiting for spindle ...
+21
-8
1 changed file
expand all
collapse all
unified
split
src
app_error.rs
+21
-8
src/app_error.rs
reviewed
···
1
1
+
use std::env;
2
2
+
1
3
use axum::{
2
4
http::StatusCode,
3
5
response::{Html, IntoResponse, Response},
···
8
10
#[error("not found")]
9
11
NotFound,
10
12
11
11
-
#[error("internal server error")]
12
12
-
DbError(#[from] sqlx::Error),
13
13
-
14
13
#[error("bad request")]
15
14
ValidationError(#[from] serde_valid::validation::Errors),
16
15
17
17
-
#[error("internal server error")]
18
18
-
Anyhow(#[from] anyhow::Error),
16
16
+
#[error(transparent)]
17
17
+
DbError(#[from] sqlx::Error),
19
18
20
20
-
#[error("internal server error")]
19
19
+
#[error(transparent)]
21
20
IoError(#[from] std::io::Error),
21
21
+
22
22
+
#[error(transparent)]
23
23
+
TemplateError(#[from] minijinja::Error),
24
24
+
25
25
+
#[error(transparent)]
26
26
+
Anyhow(#[from] anyhow::Error),
22
27
}
23
28
24
29
impl AppError {
···
26
31
match self {
27
32
Self::NotFound => StatusCode::NOT_FOUND,
28
33
Self::ValidationError(_) => StatusCode::BAD_REQUEST,
29
29
-
Self::DbError(_) | Self::Anyhow(_) | Self::IoError(_) => {
34
34
+
Self::DbError(_) | Self::Anyhow(_) | Self::IoError(_) | Self::TemplateError(_) => {
30
35
StatusCode::INTERNAL_SERVER_ERROR
31
36
}
32
37
}
···
35
40
36
41
impl IntoResponse for AppError {
37
42
fn into_response(self) -> Response {
38
38
-
(self.status_code(), Html(self.to_string())).into_response()
43
43
+
let status_code = self.status_code();
44
44
+
let is_dev = env::var("ENVIRONMENT").unwrap_or("development".to_string()) == "development";
45
45
+
let body = if status_code == StatusCode::INTERNAL_SERVER_ERROR && !is_dev {
46
46
+
"internal server error".to_string()
47
47
+
} else {
48
48
+
self.to_string()
49
49
+
};
50
50
+
51
51
+
(self.status_code(), Html(body)).into_response()
39
52
}
40
53
}