2019-06-17 19:46:21 +02:00
|
|
|
// <recommend-one>
|
|
|
|
use actix_web::{error, http, HttpResponse};
|
|
|
|
use failure::Fail;
|
|
|
|
|
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
enum UserError {
|
|
|
|
#[fail(display = "Validation error on field: {}", field)]
|
|
|
|
ValidationError { field: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::ResponseError for UserError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
|
|
|
UserError::ValidationError { .. } => {
|
|
|
|
HttpResponse::new(http::StatusCode::BAD_REQUEST)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// </recommend-one>
|
2019-06-25 03:18:30 +02:00
|
|
|
fn index() -> Result<&'static str, UserError> {
|
2019-06-19 06:20:50 +02:00
|
|
|
Err(UserError::ValidationError {
|
|
|
|
field: "bad stuff".to_string(),
|
|
|
|
})
|
|
|
|
}
|
2019-06-25 03:18:30 +02:00
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
use actix_web::{web, App, HttpServer};
|
|
|
|
|
|
|
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
|
|
|
.bind("127.0.0.1:8088")
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
|
|
|
.unwrap();
|
|
|
|
}
|