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:
8
examples/errors/Cargo.toml
Normal file
8
examples/errors/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "errors"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
failure = "0.1"
|
14
examples/errors/src/helpers.rs
Normal file
14
examples/errors/src/helpers.rs
Normal 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>
|
21
examples/errors/src/main.rs
Normal file
21
examples/errors/src/main.rs
Normal 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() {}
|
30
examples/errors/src/override_error.rs
Normal file
30
examples/errors/src/override_error.rs
Normal 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>
|
20
examples/errors/src/recommend_one.rs
Normal file
20
examples/errors/src/recommend_one.rs
Normal 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>
|
25
examples/errors/src/recommend_two.rs
Normal file
25
examples/errors/src/recommend_two.rs
Normal 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>
|
Reference in New Issue
Block a user