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

Errors section is done-ish.

This commit is contained in:
Cameron Dershem
2019-06-24 21:18:30 -04:00
parent 60f4f116cf
commit 6b08c4664b
7 changed files with 77 additions and 35 deletions

View File

@ -6,3 +6,5 @@ edition = "2018"
[dependencies]
actix-web = "1.0"
failure = "0.1"
env_logger = "0.6"
log = "0.4"

View File

@ -8,11 +8,18 @@ struct MyError {
}
pub fn index(_req: HttpRequest) -> Result<&'static str> {
let result: Result<&'static str, MyError> = Err(MyError { name: "test" });
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)
}
// </helpers>
pub fn main() {
App::new().route("/", web::get().to(index));
use actix_web::HttpServer;
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}

View File

@ -1,9 +1,10 @@
pub mod helpers;
pub mod override_error;
pub mod recommend_one;
use actix_web::{web, App};
pub mod recommend_two;
// <response-error>
use actix_web::{error, HttpRequest};
use actix_web::{error, HttpRequest, Result};
use failure::Fail;
#[derive(Fail, Debug)]
@ -19,6 +20,14 @@ fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
Err(MyError { name: "test" })
}
// </response-error>
pub fn main() {
App::new().route("/", web::get().to(index));
// use actix_web::{web, App, HttpServer};
// HttpServer::new(|| App::new().route("/", web::get().to(index)))
// .bind("127.0.0.1:8088")
// .unwrap()
// .run()
// .unwrap();
recommend_two::main();
}

View File

@ -29,12 +29,6 @@ fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
Err(MyError::BadClientData)
}
// </override>
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)
@ -43,3 +37,18 @@ fn error2(_req: HttpRequest) -> Result<&'static str, MyError> {
fn error3(_req: HttpRequest) -> Result<&'static str, MyError> {
Err(MyError::Timeout)
}
pub fn main() {
use actix_web::HttpServer;
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/e2", web::get().to(error2))
.route("/e3", web::get().to(error3))
})
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}

View File

@ -1,4 +1,3 @@
use actix_web::{web, App, HttpRequest};
// <recommend-one>
use actix_web::{error, http, HttpResponse};
use failure::Fail;
@ -19,12 +18,18 @@ impl error::ResponseError for UserError {
}
}
// </recommend-one>
pub fn main() {
App::new().route("/", web::get().to(index));
}
fn index(_req: HttpRequest) -> Result<&'static str, UserError> {
fn index() -> Result<&'static str, UserError> {
Err(UserError::ValidationError {
field: "bad stuff".to_string(),
})
}
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();
}

View File

@ -1,6 +1,5 @@
use actix_web::App;
// <recommend-two>
use actix_web::{error, fs, http, HttpRequest, HttpResponse};
use actix_web::{error, http, HttpResponse};
use failure::Fail;
#[derive(Fail, Debug)]
@ -19,11 +18,22 @@ impl error::ResponseError for UserError {
}
}
fn index(_req: HttpRequest) -> Result<&'static str, UserError> {
fs::NamedFile::open("static/index.html").map_err(|_e| UserError::InternalError)?;
fn index() -> Result<&'static str, UserError> {
do_thing_that_failes().map_err(|_e| UserError::InternalError)?;
Ok("success!")
}
// </recommend-two>
pub fn main() {
App::new().route("/", web::get().to(index));
fn do_thing_that_failes() -> Result<(), std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "some error"))
}
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();
}