2022-06-05 22:58:47 +03:00
|
|
|
use axum::{http::StatusCode, response::IntoResponse, Json};
|
|
|
|
use serde_json::value::RawValue;
|
|
|
|
|
2022-06-16 05:53:37 +03:00
|
|
|
use crate::jsonrpc::JsonRpcForwardedResponse;
|
|
|
|
|
2022-06-05 22:58:47 +03:00
|
|
|
pub async fn handler_404() -> impl IntoResponse {
|
|
|
|
let err = anyhow::anyhow!("nothing to see here");
|
|
|
|
|
2022-07-07 06:29:47 +03:00
|
|
|
handle_anyhow_error(Some(StatusCode::NOT_FOUND), None, err).await
|
2022-06-05 22:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// handle errors by converting them into something that implements `IntoResponse`
|
2022-07-23 02:26:04 +03:00
|
|
|
/// TODO: use this. i can't get <https://docs.rs/axum/latest/axum/error_handling/index.html> to work
|
2022-06-06 01:39:44 +03:00
|
|
|
/// TODO: i think we want a custom result type instead. put the anyhow result inside. then `impl IntoResponse for CustomResult`
|
2022-06-05 22:58:47 +03:00
|
|
|
pub async fn handle_anyhow_error(
|
|
|
|
code: Option<StatusCode>,
|
2022-07-07 06:29:47 +03:00
|
|
|
id: Option<Box<RawValue>>,
|
2022-07-07 06:22:09 +03:00
|
|
|
err: anyhow::Error,
|
2022-06-05 22:58:47 +03:00
|
|
|
) -> impl IntoResponse {
|
2022-07-07 06:29:47 +03:00
|
|
|
// TODO: we might have an id. like if this is for rate limiting, we can use it
|
|
|
|
let id = id.unwrap_or_else(|| RawValue::from_string("null".to_string()).unwrap());
|
2022-06-05 22:58:47 +03:00
|
|
|
|
|
|
|
let err = JsonRpcForwardedResponse::from_anyhow_error(err, id);
|
|
|
|
|
2022-07-07 06:29:47 +03:00
|
|
|
// TODO: logs here are too verbose. emit a stat
|
|
|
|
// warn!("Responding with error: {:?}", err);
|
2022-06-05 22:58:47 +03:00
|
|
|
|
|
|
|
let code = code.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
(code, Json(err))
|
|
|
|
}
|