1
0
mirror of https://github.com/actix/actix-website synced 2024-12-19 02:13:13 +01:00
actix-website/examples/errors/src/recommend_one.rs

31 lines
765 B
Rust
Raw Normal View History

use actix_web::{web, App, HttpRequest};
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>
pub fn main() {
App::new().route("/", web::get().to(index));
}
fn index(_req: HttpRequest) -> Result<&'static str, UserError> {
Err(UserError::ValidationError {
field: "bad stuff".to_string(),
})
}