// use actix_web::{ dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse, HttpServer, }; use derive_more::{Display, Error}; #[derive(Debug, Display, Error)] enum UserError { #[display(fmt = "An internal error occurred. Please try again later.")] InternalError, } impl error::ResponseError for UserError { 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 { UserError::InternalError => StatusCode::INTERNAL_SERVER_ERROR, } } } #[get("/")] async fn index() -> Result<&'static str, UserError> { do_thing_that_fails().map_err(|_e| UserError::InternalError)?; Ok("success!") } // fn do_thing_that_fails() -> Result<(), std::io::Error> { Err(std::io::Error::new(std::io::ErrorKind::Other, "some error")) } #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new().service(index)) .bind("127.0.0.1:8080")? .run() .await }