2019-06-19 06:20:50 +02:00
|
|
|
use actix_web::{web, App};
|
2019-06-17 19:46:21 +02:00
|
|
|
// <override>
|
|
|
|
use actix_web::{error, http, HttpRequest, HttpResponse};
|
|
|
|
use failure::Fail;
|
|
|
|
|
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
enum MyError {
|
|
|
|
#[fail(display = "internal error")]
|
|
|
|
InternalError,
|
|
|
|
#[fail(display = "bad request")]
|
|
|
|
BadClientData,
|
|
|
|
#[fail(display = "timeout")]
|
|
|
|
Timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::ResponseError for MyError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
|
|
|
MyError::InternalError => {
|
|
|
|
HttpResponse::new(http::StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
MyError::BadClientData => HttpResponse::new(http::StatusCode::BAD_REQUEST),
|
|
|
|
MyError::Timeout => HttpResponse::new(http::StatusCode::GATEWAY_TIMEOUT),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:20:50 +02:00
|
|
|
fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
|
2019-06-17 19:46:21 +02:00
|
|
|
Err(MyError::BadClientData)
|
|
|
|
}
|
|
|
|
// </override>
|
2019-06-19 06:20:50 +02:00
|
|
|
pub fn main() {
|
|
|
|
App::new()
|
|
|
|
.route("/", web::get().to(index))
|
|
|
|
.route("/e2", web::get().to(error2))
|
|
|
|
.route("/e3", web::get().to(error3));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error2(_req: HttpRequest) -> Result<&'static str, MyError> {
|
|
|
|
Err(MyError::InternalError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error3(_req: HttpRequest) -> Result<&'static str, MyError> {
|
|
|
|
Err(MyError::Timeout)
|
|
|
|
}
|