Work on axum support

This commit is contained in:
Lennart
2025-06-07 20:17:50 +02:00
parent 57832116aa
commit 790c657b08
38 changed files with 582 additions and 64 deletions

View File

@@ -55,17 +55,40 @@ impl Error {
#[cfg(feature = "actix")]
impl actix_web::error::ResponseError for Error {
fn status_code(&self) -> StatusCode {
fn status_code(&self) -> actix_web::http::StatusCode {
self.status_code()
.as_u16()
.try_into()
.expect("Just converting between versions")
}
fn error_response(&self) -> actix_web::HttpResponse {
use actix_web::ResponseError;
error!("Error: {self}");
match self {
Error::Unauthorized => actix_web::HttpResponse::build(self.status_code())
Error::Unauthorized => actix_web::HttpResponse::build(ResponseError::status_code(self))
.append_header(("WWW-Authenticate", "Basic"))
.body(self.to_string()),
_ => actix_web::HttpResponse::build(self.status_code()).body(self.to_string()),
_ => actix_web::HttpResponse::build(ResponseError::status_code(self))
.body(self.to_string()),
}
}
}
#[cfg(feature = "axum")]
impl axum::response::IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
use axum::body::Body;
let mut resp = axum::response::Response::builder().status(self.status_code());
if matches!(&self, &Error::Unauthorized) {
resp.headers_mut()
.expect("This must always work")
.insert("WWW-Authenticate", "Basic".parse().unwrap());
}
resp.body(Body::new(self.to_string()))
.expect("This should always work")
}
}