2019-06-17 19:46:21 +02:00
|
|
|
// <recommend-two>
|
2019-06-25 03:18:30 +02:00
|
|
|
use actix_web::{error, http, HttpResponse};
|
2019-06-17 19:46:21 +02:00
|
|
|
use failure::Fail;
|
|
|
|
|
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
enum UserError {
|
|
|
|
#[fail(display = "An internal error occurred. Please try again later.")]
|
|
|
|
InternalError,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::ResponseError for UserError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
|
|
|
UserError::InternalError => {
|
|
|
|
HttpResponse::new(http::StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 03:18:30 +02:00
|
|
|
fn index() -> Result<&'static str, UserError> {
|
|
|
|
do_thing_that_failes().map_err(|_e| UserError::InternalError)?;
|
2019-06-17 19:46:21 +02:00
|
|
|
Ok("success!")
|
|
|
|
}
|
|
|
|
// </recommend-two>
|
2019-06-25 03:18:30 +02:00
|
|
|
|
|
|
|
fn do_thing_that_failes() -> Result<(), std::io::Error> {
|
|
|
|
Err(std::io::Error::new(std::io::ErrorKind::Other, "some error"))
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:20:50 +02:00
|
|
|
pub fn main() {
|
2019-06-25 03:18:30 +02:00
|
|
|
use actix_web::{web, App, HttpServer};
|
|
|
|
|
|
|
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
|
|
|
.bind("127.0.0.1:8088")
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
|
|
|
.unwrap();
|
2019-06-19 06:20:50 +02:00
|
|
|
}
|