1
0
mirror of https://github.com/actix/actix-website synced 2025-01-23 00:25:55 +01:00
2022-02-26 03:56:24 +00:00

35 lines
950 B
Rust

#![allow(dead_code)]
// <error-handler>
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{
dev,
http::{header, StatusCode},
web, App, HttpResponse, HttpServer, Result,
};
fn add_error_header<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
res.response_mut().headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("Error"),
);
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(
ErrorHandlers::new()
.handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header),
)
.service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
// </error-handler>