pub fn adapt_exception_handler(
handler: Arc<dyn ExceptionHandler>,
) -> Arc<dyn HttpExceptionHandler> ⓘExpand description
Adapts a legacy ExceptionHandler to the framework-wide HTTP hook.
Error::NotFound and Error::Http retain their corresponding legacy
categories. Error variants without a corresponding DispatchError
variant are represented as legacy view errors. The original dispatch
categories remain available to existing implementations, while new code
should implement reinhardt_http::ExceptionHandler directly.
§Example
use std::sync::Arc;
use async_trait::async_trait;
use hyper::StatusCode;
use reinhardt_core::exception::Error;
use reinhardt_dispatch::{adapt_exception_handler, DispatchError, ExceptionHandler};
use reinhardt_http::{Request, Response};
struct MyDispatchErrors;
#[async_trait]
impl ExceptionHandler for MyDispatchErrors {
async fn handle_exception(&self, _request: &Request, error: DispatchError) -> Response {
let status = match error {
DispatchError::UrlResolution(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
Response::new(status)
}
}
#[tokio::main]
async fn main() {
let legacy: Arc<dyn ExceptionHandler> = Arc::new(MyDispatchErrors);
let http_handler = adapt_exception_handler(legacy);
let request = Request::builder().uri("/missing").build().unwrap();
let response = http_handler
.handle_exception(&request, Error::NotFound("route not found".into()))
.await;
assert_eq!(response.status, StatusCode::NOT_FOUND);
}