2022-09-22 22:57:21 +03:00
|
|
|
use crate::{app::UserData, jsonrpc::JsonRpcForwardedResponse};
|
2022-08-10 05:37:34 +03:00
|
|
|
use axum::{
|
|
|
|
http::StatusCode,
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
Json,
|
|
|
|
};
|
2022-08-17 00:43:39 +03:00
|
|
|
use derive_more::From;
|
2022-09-15 20:57:24 +03:00
|
|
|
use redis_rate_limiter::redis::RedisError;
|
2022-08-27 08:42:25 +03:00
|
|
|
use sea_orm::DbErr;
|
2022-09-10 03:12:14 +03:00
|
|
|
use std::{error::Error, net::IpAddr};
|
|
|
|
use tokio::time::Instant;
|
2022-09-22 22:57:21 +03:00
|
|
|
use tracing::warn;
|
2022-06-05 22:58:47 +03:00
|
|
|
|
2022-08-17 00:43:39 +03:00
|
|
|
// TODO: take "IntoResult" instead?
|
|
|
|
pub type FrontendResult = Result<Response, FrontendErrorResponse>;
|
|
|
|
|
|
|
|
#[derive(From)]
|
|
|
|
pub enum FrontendErrorResponse {
|
|
|
|
Anyhow(anyhow::Error),
|
2022-08-17 01:52:12 +03:00
|
|
|
Box(Box<dyn Error>),
|
|
|
|
Redis(RedisError),
|
2022-08-21 12:39:38 +03:00
|
|
|
Response(Response),
|
2022-08-27 08:42:25 +03:00
|
|
|
Database(DbErr),
|
2022-09-22 22:57:21 +03:00
|
|
|
RateLimitedUser(UserData, Option<Instant>),
|
2022-09-10 03:12:14 +03:00
|
|
|
RateLimitedIp(IpAddr, Option<Instant>),
|
2022-09-12 17:33:55 +03:00
|
|
|
UnknownKey,
|
2022-09-10 03:12:14 +03:00
|
|
|
NotFound,
|
2022-08-16 22:29:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:43:39 +03:00
|
|
|
impl IntoResponse for FrontendErrorResponse {
|
2022-08-16 22:29:00 +03:00
|
|
|
fn into_response(self) -> Response {
|
2022-09-10 03:12:14 +03:00
|
|
|
// TODO: include the request id in these so that users can give us something that will point to logs
|
|
|
|
let (status_code, response) = match self {
|
|
|
|
Self::Anyhow(err) => {
|
|
|
|
warn!(?err, "anyhow");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
2022-09-10 03:58:33 +03:00
|
|
|
JsonRpcForwardedResponse::from_string(
|
2022-09-20 06:26:12 +03:00
|
|
|
// TODO: is it safe to expose all of our anyhow strings?
|
2022-09-10 03:58:33 +03:00
|
|
|
err.to_string(),
|
2022-09-10 03:12:14 +03:00
|
|
|
Some(StatusCode::INTERNAL_SERVER_ERROR.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Box(err) => {
|
|
|
|
warn!(?err, "boxed");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcForwardedResponse::from_str(
|
2022-09-20 06:26:12 +03:00
|
|
|
// TODO: make this better. maybe include the error type?
|
2022-09-10 03:12:14 +03:00
|
|
|
"boxed error!",
|
|
|
|
Some(StatusCode::INTERNAL_SERVER_ERROR.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Redis(err) => {
|
|
|
|
warn!(?err, "redis");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcForwardedResponse::from_str(
|
|
|
|
"redis error!",
|
|
|
|
Some(StatusCode::INTERNAL_SERVER_ERROR.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-08-21 12:39:38 +03:00
|
|
|
Self::Response(r) => {
|
2022-09-10 03:12:14 +03:00
|
|
|
debug_assert_ne!(r.status(), StatusCode::OK);
|
2022-08-21 12:39:38 +03:00
|
|
|
return r;
|
|
|
|
}
|
2022-09-10 03:12:14 +03:00
|
|
|
Self::Database(err) => {
|
|
|
|
warn!(?err, "database");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcForwardedResponse::from_str(
|
|
|
|
"database error!",
|
|
|
|
Some(StatusCode::INTERNAL_SERVER_ERROR.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::RateLimitedIp(ip, retry_at) => {
|
|
|
|
// TODO: emit a stat
|
|
|
|
// TODO: include retry_at in the error
|
2022-09-20 06:26:12 +03:00
|
|
|
// TODO: if retry_at is None, give an unauthorized status code?
|
2022-09-10 03:12:14 +03:00
|
|
|
(
|
|
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
|
|
JsonRpcForwardedResponse::from_string(
|
|
|
|
format!("too many requests from ip {}!", ip),
|
|
|
|
Some(StatusCode::TOO_MANY_REQUESTS.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// TODO: this should actually by the id of the key. multiple users might control one key
|
2022-09-22 22:57:21 +03:00
|
|
|
Self::RateLimitedUser(user_data, retry_at) => {
|
2022-09-10 03:12:14 +03:00
|
|
|
// TODO: emit a stat
|
|
|
|
// TODO: include retry_at in the error
|
|
|
|
(
|
|
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
|
|
JsonRpcForwardedResponse::from_string(
|
2022-09-22 22:57:21 +03:00
|
|
|
// TODO: better error
|
|
|
|
format!("too many requests from {:?}!", user_data),
|
2022-09-10 03:12:14 +03:00
|
|
|
Some(StatusCode::TOO_MANY_REQUESTS.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-09-12 17:33:55 +03:00
|
|
|
Self::UnknownKey => (
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
JsonRpcForwardedResponse::from_str(
|
|
|
|
"unknown api key!",
|
|
|
|
Some(StatusCode::UNAUTHORIZED.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
),
|
2022-09-10 03:12:14 +03:00
|
|
|
Self::NotFound => {
|
|
|
|
// TODO: emit a stat?
|
2022-09-20 06:26:12 +03:00
|
|
|
// TODO: instead of an error, show a normal html page for 404
|
2022-09-10 03:12:14 +03:00
|
|
|
(
|
|
|
|
StatusCode::NOT_FOUND,
|
|
|
|
JsonRpcForwardedResponse::from_str(
|
|
|
|
"not found!",
|
|
|
|
Some(StatusCode::NOT_FOUND.as_u16().into()),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-08-17 01:52:12 +03:00
|
|
|
};
|
|
|
|
|
2022-09-10 03:12:14 +03:00
|
|
|
(status_code, Json(response)).into_response()
|
2022-08-16 22:29:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-16 05:53:37 +03:00
|
|
|
|
2022-08-10 05:37:34 +03:00
|
|
|
pub async fn handler_404() -> Response {
|
2022-09-10 03:12:14 +03:00
|
|
|
FrontendErrorResponse::NotFound.into_response()
|
2022-06-05 22:58:47 +03:00
|
|
|
}
|