mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 13:06:50 +00:00
Some checks failed
Build dashboard Docker image / dashboard docker build (push) Has been cancelled
Build and push Docker image / .net docker build (push) Has been cancelled
.net checks / run .net tests (push) Has been cancelled
.net checks / dotnet-format (push) Has been cancelled
Build and push Rust service Docker images / rust docker build (push) Has been cancelled
rust checks / cargo fmt (push) Has been cancelled
61 lines
1.9 KiB
Rust
61 lines
1.9 KiB
Rust
use crate::error::PKError;
|
|
use axum::{
|
|
http::{HeaderValue, StatusCode},
|
|
response::IntoResponse,
|
|
};
|
|
use serde_json::{Value, json, to_string};
|
|
use tracing::error;
|
|
|
|
pub fn header_or_unknown(header: Option<&HeaderValue>) -> &str {
|
|
if let Some(value) = header {
|
|
match value.to_str() {
|
|
Ok(v) => v,
|
|
Err(err) => {
|
|
error!(?err, ?value, "failed to parse header value");
|
|
"failed to parse"
|
|
}
|
|
}
|
|
} else {
|
|
"unknown"
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn wrapper<F>(handler: F) -> impl Fn() -> axum::response::Response
|
|
where
|
|
F: Fn() -> anyhow::Result<Value>,
|
|
{
|
|
move || match handler() {
|
|
Ok(v) => (StatusCode::OK, to_string(&v).unwrap()).into_response(),
|
|
Err(error) => match error.downcast_ref::<PKError>() {
|
|
Some(pkerror) => json_err(
|
|
pkerror.response_code,
|
|
to_string(&json!({ "message": pkerror.message, "code": pkerror.json_code }))
|
|
.unwrap(),
|
|
),
|
|
None => {
|
|
error!(?error, "error in handler {}", std::any::type_name::<F>(),);
|
|
json_err(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
r#"{"message": "500: Internal Server Error", "code": 0}"#.to_string(),
|
|
)
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn handle_panic(error: Box<dyn std::any::Any + Send + 'static>) -> axum::response::Response {
|
|
error!(?error, "caught panic from handler");
|
|
json_err(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
r#"{"message": "500: Internal Server Error", "code": 0}"#.to_string(),
|
|
)
|
|
}
|
|
|
|
// todo: make 500 not duplicated
|
|
pub fn json_err(code: StatusCode, text: String) -> axum::response::Response {
|
|
let mut response = (code, text).into_response();
|
|
let headers = response.headers_mut();
|
|
headers.insert("content-type", HeaderValue::from_static("application/json"));
|
|
response
|
|
}
|