-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.rs
150 lines (122 loc) · 4.05 KB
/
err.rs
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
/* SPDX-License-Identifier: CC0-1.0
*
* src/err.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
// Error generation and response stuff, used for handlers.
use askama_axum::Template;
use axum::{
body::Body,
http::StatusCode,
response::{IntoResponse, Response},
};
use tracing::{error, warn};
use crate::{
auth::{AuthError, Backend},
bancache::BanCacheError,
csrf::SessionError,
urlcache::UrlCacheError,
util::net::{AddressError, NetworkPrefixError},
};
// Anything that can go wrong in a handler should go here.
#[derive(Debug, thiserror::Error)]
pub enum AppError {
#[error(transparent)]
VerifyCsrf(#[from] SessionError),
#[error(transparent)]
Auth(#[from] AuthError),
#[error(transparent)]
AxumLogin(#[from] axum_login::Error<Backend>),
#[error(transparent)]
Session(#[from] tower_sessions::session::Error),
#[error(transparent)]
Db(#[from] sea_orm::DbErr),
#[error(transparent)]
BanCache(#[from] BanCacheError),
#[error(transparent)]
UrlCache(#[from] UrlCacheError),
#[error(transparent)]
Address(#[from] AddressError),
#[error(transparent)]
NetworkPrefix(#[from] NetworkPrefixError),
#[error("Could not validate URL {}: {}", .0, .1)]
UrlValidation(String, String),
#[error(transparent)]
Regex(#[from] regex::Error),
#[error("Not found")]
NotFound,
#[error("Unauthorized")]
Unauthorized,
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
Self::VerifyCsrf(e) => {
warn!("CSRF token verification failed");
ErrorResponse::bad_request(e.to_string().as_ref())
}
Self::UrlValidation(url, error_reason) => {
ErrorResponse::url_submission(&url, &error_reason)
}
Self::NotFound => ErrorResponse::not_found(),
Self::Unauthorized => ErrorResponse::unauthorized(),
_ => {
// If it's anything else, 500.
error!("Internal server error: {}", self.to_string());
ErrorResponse::internal_server_error(self.to_string().as_str())
}
}
}
}
#[derive(Template)]
#[template(path = "errors/code/400.html")]
struct BadRequestTemplate<'a> {
error_reason: &'a str,
}
#[derive(Template)]
#[template(path = "errors/code/403.html")]
struct UnauthorizedTemplate;
#[derive(Template)]
#[template(path = "errors/code/404.html")]
struct NotFoundTemplate;
#[derive(Template)]
#[template(path = "errors/code/500.html")]
struct InternalServerErrorTemplate<'a> {
error_reason: &'a str,
}
#[derive(Template)]
#[template(path = "errors/form/url.html")]
struct UrlSubmissionErrorTemplate<'a> {
error_reason: &'a str,
url: &'a str,
}
// This returns various canned responses for various issues.
pub struct ErrorResponse;
impl ErrorResponse {
pub(crate) fn bad_request(error_reason: &str) -> Response<Body> {
let t = BadRequestTemplate { error_reason };
(StatusCode::BAD_REQUEST, t).into_response()
}
pub(crate) fn unauthorized() -> Response<Body> {
(StatusCode::UNAUTHORIZED, UnauthorizedTemplate).into_response()
}
pub(crate) fn not_found() -> Response<Body> {
(StatusCode::NOT_FOUND, NotFoundTemplate).into_response()
}
pub(crate) fn internal_server_error(error_reason: &str) -> Response<Body> {
let t = InternalServerErrorTemplate { error_reason };
(StatusCode::INTERNAL_SERVER_ERROR, t).into_response()
}
pub(crate) fn url_submission<'a>(url: &'a str, error_reason: &'a str) -> Response<Body> {
let t = UrlSubmissionErrorTemplate { error_reason, url };
(StatusCode::UNPROCESSABLE_ENTITY, t).into_response()
}
}