1
0
mirror of https://github.com/actix/actix-website synced 2025-02-02 12:19:04 +01:00

35 lines
950 B
Rust
Raw Normal View History

2020-09-12 16:21:54 +01:00
#![allow(dead_code)]
2019-06-17 16:57:57 -04:00
// <error-handler>
2022-02-26 03:56:24 +00:00
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{
dev,
http::{header, StatusCode},
web, App, HttpResponse, HttpServer, Result,
};
2019-06-17 16:57:57 -04:00
2021-12-22 13:36:35 -05:00
fn add_error_header<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
2019-06-17 16:57:57 -04:00
res.response_mut().headers_mut().insert(
2022-02-26 03:56:24 +00:00
header::CONTENT_TYPE,
header::HeaderValue::from_static("Error"),
2019-06-17 16:57:57 -04:00
);
2022-02-26 03:56:24 +00:00
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
2019-06-17 16:57:57 -04:00
}
2020-09-12 16:21:54 +01:00
#[actix_web::main]
2019-12-29 02:03:17 +09:00
async fn main() -> std::io::Result<()> {
2019-06-26 02:59:20 -04:00
HttpServer::new(|| {
App::new()
2022-02-26 03:56:24 +00:00
.wrap(
ErrorHandlers::new()
.handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header),
2019-06-26 02:59:20 -04:00
)
2022-02-26 03:56:24 +00:00
.service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)))
2019-06-26 02:59:20 -04:00
})
2022-02-26 03:56:24 +00:00
.bind(("127.0.0.1", 8080))?
2019-06-26 02:59:20 -04:00
.run()
2019-12-29 02:03:17 +09:00
.await
2019-06-17 16:57:57 -04:00
}
// </error-handler>