1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00
This commit is contained in:
Rob Ede
2020-09-12 16:21:54 +01:00
committed by GitHub
parent a0ce9f28e2
commit 4d8d53cea5
145 changed files with 1011 additions and 1461 deletions

View File

@ -4,9 +4,8 @@ version = "1.0.0"
edition = "2018"
[dependencies]
actix-web = "2.0.0"
actix-rt = "1.0.0"
env_logger = "0.7.1"
log = "0.4.8"
failure = "0.1.6"
actix-http = "1.0.1"
actix-web = "3"
derive_more = "0.99"
env_logger = "0.7"
log = "0.4"
# actix-http = "1"

View File

@ -1,12 +1,12 @@
use actix_web::{web, App};
// <helpers>
use actix_web::{error, Result};
use actix_web::{error, get, App, HttpServer, Result};
#[derive(Debug)]
struct MyError {
name: &'static str,
}
#[get("/")]
async fn index() -> Result<&'static str> {
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
@ -14,12 +14,10 @@ async fn index() -> Result<&'static str> {
}
// </helpers>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,10 +1,10 @@
// <logging>
use actix_web::{error, Result};
use failure::Fail;
use actix_web::{error, get, middleware::Logger, App, HttpServer, Result};
use log::debug;
use derive_more::{Display, Error};
#[derive(Fail, Debug)]
#[fail(display = "my error")]
#[derive(Debug, Display, Error)]
#[display(fmt = "my error: {}", name)]
pub struct MyError {
name: &'static str,
}
@ -12,27 +12,22 @@ pub struct MyError {
// Use default implementation for `error_response()` method
impl error::ResponseError for MyError {}
#[get("/")]
async fn index() -> Result<&'static str, MyError> {
let err = MyError { name: "test error" };
debug!("{}", err);
Err(err)
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{middleware::Logger, web, App, HttpServer};
std::env::set_var("RUST_LOG", "my_errors=debug,actix_web=info");
std::env::set_var("RUST_BACKTRACE", "1");
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.route("/", web::get().to(index))
})
.bind("127.0.0.1:8088")?
.run()
.await
HttpServer::new(|| App::new().wrap(Logger::default()).service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}
// </logging>

View File

@ -6,10 +6,10 @@ pub mod recommend_two;
// <response-error>
use actix_web::{error, Result};
use failure::Fail;
use derive_more::{Display, Error};
#[derive(Fail, Debug)]
#[fail(display = "my error")]
#[derive(Debug, Display, Error)]
#[display(fmt = "my error: {}", name)]
struct MyError {
name: &'static str,
}
@ -22,12 +22,12 @@ async fn index() -> Result<&'static str, MyError> {
}
// </response-error>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,22 +1,24 @@
use actix_web::{web, App};
// <override>
use actix_http::ResponseBuilder;
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
use failure::Fail;
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
};
use derive_more::{Display, Error};
#[derive(Fail, Debug)]
#[derive(Debug, Display, Error)]
enum MyError {
#[fail(display = "internal error")]
#[display(fmt = "internal error")]
InternalError,
#[fail(display = "bad request")]
#[display(fmt = "bad request")]
BadClientData,
#[fail(display = "timeout")]
#[display(fmt = "timeout")]
Timeout,
}
impl error::ResponseError for MyError {
fn error_response(&self) -> HttpResponse {
ResponseBuilder::new(self.status_code())
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(self.to_string())
}
@ -30,30 +32,28 @@ impl error::ResponseError for MyError {
}
}
#[get("/")]
async fn index() -> Result<&'static str, MyError> {
Err(MyError::BadClientData)
}
// </override>
#[get("/e2")]
async fn error2() -> Result<&'static str, MyError> {
Err(MyError::InternalError)
}
#[get("/e3")]
async fn error3() -> Result<&'static str, MyError> {
Err(MyError::Timeout)
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
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")?
.run()
.await
HttpServer::new(|| App::new().service(index).service(error2).service(error3))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,17 +1,19 @@
// <recommend-one>
use actix_http::ResponseBuilder;
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
use failure::Fail;
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
HttpServer,
};
use derive_more::{Display, Error};
#[derive(Fail, Debug)]
#[derive(Debug, Display, Error)]
enum UserError {
#[fail(display = "Validation error on field: {}", field)]
#[display(fmt = "Validation error on field: {}", field)]
ValidationError { field: String },
}
impl error::ResponseError for UserError {
fn error_response(&self) -> HttpResponse {
ResponseBuilder::new(self.status_code())
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(self.to_string())
}
@ -22,18 +24,18 @@ impl error::ResponseError for UserError {
}
}
// </recommend-one>
#[get("/")]
async fn index() -> Result<&'static str, UserError> {
Err(UserError::ValidationError {
field: "bad stuff".to_string(),
})
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,17 +1,19 @@
// <recommend-two>
use actix_http::ResponseBuilder;
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
use failure::Fail;
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
HttpServer,
};
use derive_more::{Display, Error};
#[derive(Fail, Debug)]
#[derive(Debug, Display, Error)]
enum UserError {
#[fail(display = "An internal error occurred. Please try again later.")]
#[display(fmt = "An internal error occurred. Please try again later.")]
InternalError,
}
impl error::ResponseError for UserError {
fn error_response(&self) -> HttpResponse {
ResponseBuilder::new(self.status_code())
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(self.to_string())
}
@ -22,22 +24,21 @@ impl error::ResponseError for UserError {
}
}
#[get("/")]
async fn index() -> Result<&'static str, UserError> {
do_thing_that_failes().map_err(|_e| UserError::InternalError)?;
do_thing_that_fails().map_err(|_e| UserError::InternalError)?;
Ok("success!")
}
// </recommend-two>
fn do_thing_that_failes() -> Result<(), std::io::Error> {
fn do_thing_that_fails() -> Result<(), std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "some error"))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}