1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

First pass at errors section.

This commit is contained in:
Cameron Dershem
2019-06-17 13:46:21 -04:00
parent 71e6f076a4
commit f07c78a5ca
8 changed files with 124 additions and 107 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "errors"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "1.0"
failure = "0.1"

View File

@ -0,0 +1,14 @@
// <helpers>
use actix_web::{error, HttpRequest, Result};
#[derive(Debug)]
struct MyError {
name: &'static str,
}
fn index(req: &HttpRequest) -> Result<&'static str> {
let result: Result<&'static str, MyError> = Err(MyError { name: "test" });
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)
}
// </helpers>

View File

@ -0,0 +1,21 @@
mod helpers;
mod override_error;
mod recommend_one;
// <response-error>
use actix_web::{error, HttpRequest};
use failure::Fail;
#[derive(Fail, Debug)]
#[fail(display = "my error")]
struct MyError {
name: &'static str,
}
// Use default implementation for `error_response()` method
impl error::ResponseError for MyError {}
fn index(req: HttpRequest) -> Result<&'static str, MyError> {
Err(MyError { name: "test" })
}
// </response-error>
fn main() {}

View File

@ -0,0 +1,30 @@
// <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),
}
}
}
fn index(req: &HttpRequest) -> Result<&'static str, MyError> {
Err(MyError::BadClientData)
}
// </override>

View File

@ -0,0 +1,20 @@
// <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>

View File

@ -0,0 +1,25 @@
// <recommend-two>
use actix_web::{error, fs, http, App, HttpRequest, HttpResponse};
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)
}
}
}
}
fn index(_req: HttpRequest) -> Result<&'static str, UserError> {
fs::NamedFile::open("static/index.html").map_err(|_e| UserError::InternalError)?;
Ok("success!")
}
// </recommend-two>