// use actix_web::{ dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse, }; use derive_more::{Display, Error}; #[derive(Debug, Display, Error)] enum MyError { #[display(fmt = "internal error")] InternalError, #[display(fmt = "bad request")] BadClientData, #[display(fmt = "timeout")] Timeout, } impl error::ResponseError for MyError { fn error_response(&self) -> HttpResponse { HttpResponseBuilder::new(self.status_code()) .set_header(header::CONTENT_TYPE, "text/html; charset=utf-8") .body(self.to_string()) } fn status_code(&self) -> StatusCode { match *self { MyError::InternalError => StatusCode::INTERNAL_SERVER_ERROR, MyError::BadClientData => StatusCode::BAD_REQUEST, MyError::Timeout => StatusCode::GATEWAY_TIMEOUT, } } } #[get("/")] async fn index() -> Result<&'static str, MyError> { Err(MyError::BadClientData) } // #[get("/e2")] async fn error2() -> Result<&'static str, MyError> { Err(MyError::InternalError) } #[get("/e3")] async fn error3() -> Result<&'static str, MyError> { Err(MyError::Timeout) } #[actix_web::main] async fn main() -> std::io::Result<()> { use actix_web::HttpServer; HttpServer::new(|| App::new().service(index).service(error2).service(error3)) .bind("127.0.0.1:8080")? .run() .await }