mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 01:12:24 +00:00
Work on axum support
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user