mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
Update examples/{extractors, errors} to futures 0.3 and actix-web 2.0 (#130)
* Update to futures 0.3 * update examples/errors to actix-web 2.0
This commit is contained in:
@ -4,7 +4,9 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
failure = "0.1"
|
||||
env_logger = "0.6"
|
||||
log = "0.4"
|
||||
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"
|
||||
|
@ -7,19 +7,19 @@ struct MyError {
|
||||
name: &'static str,
|
||||
}
|
||||
|
||||
pub fn index() -> Result<&'static str> {
|
||||
async fn index() -> Result<&'static str> {
|
||||
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
|
||||
|
||||
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)
|
||||
}
|
||||
// </helpers>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::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")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -12,13 +12,14 @@ pub struct MyError {
|
||||
// Use default implementation for `error_response()` method
|
||||
impl error::ResponseError for MyError {}
|
||||
|
||||
pub fn index() -> Result<&'static str, MyError> {
|
||||
async fn index() -> Result<&'static str, MyError> {
|
||||
let err = MyError { name: "test error" };
|
||||
debug!("{}", err);
|
||||
Err(err)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::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");
|
||||
@ -30,9 +31,8 @@ pub fn main() {
|
||||
.wrap(Logger::default())
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </logging>
|
||||
|
@ -10,24 +10,24 @@ use failure::Fail;
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "my error")]
|
||||
pub struct MyError {
|
||||
struct MyError {
|
||||
name: &'static str,
|
||||
}
|
||||
|
||||
// Use default implementation for `error_response()` method
|
||||
impl error::ResponseError for MyError {}
|
||||
|
||||
fn index() -> Result<&'static str, MyError> {
|
||||
async fn index() -> Result<&'static str, MyError> {
|
||||
Err(MyError { name: "test" })
|
||||
}
|
||||
// </response-error>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::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")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use actix_web::{web, App};
|
||||
// <override>
|
||||
use actix_web::{error, http, HttpResponse};
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
@ -15,30 +16,35 @@ enum MyError {
|
||||
|
||||
impl error::ResponseError for MyError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
|
||||
fn status_code(&self) -> StatusCode {
|
||||
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),
|
||||
MyError::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
MyError::BadClientData => StatusCode::BAD_REQUEST,
|
||||
MyError::Timeout => StatusCode::GATEWAY_TIMEOUT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn index() -> Result<&'static str, MyError> {
|
||||
async fn index() -> Result<&'static str, MyError> {
|
||||
Err(MyError::BadClientData)
|
||||
}
|
||||
// </override>
|
||||
|
||||
fn error2() -> Result<&'static str, MyError> {
|
||||
async fn error2() -> Result<&'static str, MyError> {
|
||||
Err(MyError::InternalError)
|
||||
}
|
||||
|
||||
fn error3() -> Result<&'static str, MyError> {
|
||||
async fn error3() -> Result<&'static str, MyError> {
|
||||
Err(MyError::Timeout)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -47,8 +53,7 @@ pub fn main() {
|
||||
.route("/e2", web::get().to(error2))
|
||||
.route("/e3", web::get().to(error3))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
// <recommend-one>
|
||||
use actix_web::{error, http, HttpResponse};
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
@ -10,26 +11,29 @@ enum UserError {
|
||||
|
||||
impl error::ResponseError for UserError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
UserError::ValidationError { .. } => {
|
||||
HttpResponse::new(http::StatusCode::BAD_REQUEST)
|
||||
}
|
||||
UserError::ValidationError { .. } => StatusCode::BAD_REQUEST,
|
||||
}
|
||||
}
|
||||
}
|
||||
// </recommend-one>
|
||||
fn index() -> Result<&'static str, UserError> {
|
||||
async fn index() -> Result<&'static str, UserError> {
|
||||
Err(UserError::ValidationError {
|
||||
field: "bad stuff".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::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")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
// <recommend-two>
|
||||
use actix_web::{error, http, HttpResponse};
|
||||
use actix_http::ResponseBuilder;
|
||||
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
||||
use failure::Fail;
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
@ -10,15 +11,18 @@ enum UserError {
|
||||
|
||||
impl error::ResponseError for UserError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
ResponseBuilder::new(self.status_code())
|
||||
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(self.to_string())
|
||||
}
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
UserError::InternalError => {
|
||||
HttpResponse::new(http::StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
UserError::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn index() -> Result<&'static str, UserError> {
|
||||
async fn index() -> Result<&'static str, UserError> {
|
||||
do_thing_that_failes().map_err(|_e| UserError::InternalError)?;
|
||||
Ok("success!")
|
||||
}
|
||||
@ -28,12 +32,12 @@ fn do_thing_that_failes() -> Result<(), std::io::Error> {
|
||||
Err(std::io::Error::new(std::io::ErrorKind::Other, "some error"))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::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")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user