mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 05:41:50 +01:00
Add ResponseError impl for SendRequestError
This commit is contained in:
parent
ea2a8f6908
commit
b282ec106e
@ -1,5 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## 0.4.5 (2018-03-xx)
|
||||||
|
|
||||||
|
* Add `ResponseError` impl for `SendRequestError`.
|
||||||
|
This improves ergonomics of http client.
|
||||||
|
|
||||||
|
|
||||||
## 0.4.4 (2018-03-04)
|
## 0.4.4 (2018-03-04)
|
||||||
|
|
||||||
* Allow to use Arc<Vec<u8>> as response/request body
|
* Allow to use Arc<Vec<u8>> as response/request body
|
||||||
|
@ -4,40 +4,52 @@ extern crate futures;
|
|||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use futures::Future;
|
use futures::{Future, Stream};
|
||||||
use futures::future::{ok, err, Either};
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Stream client request response and then send body to a server response
|
||||||
fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
||||||
.finish().unwrap()
|
.finish().unwrap()
|
||||||
.send()
|
.send()
|
||||||
.map_err(|e| error::Error::from(error::ErrorInternalServerError(e)))
|
.map_err(error::Error::from) // <- convert SendRequestError to an Error
|
||||||
.then(|result| match result {
|
.and_then(
|
||||||
Ok(resp) => {
|
|resp| resp.body() // <- this is MessageBody type, resolves to complete body
|
||||||
Either::A(resp.body().from_err().and_then(|body| {
|
.from_err() // <- convet PayloadError to a Error
|
||||||
match httpcodes::HttpOk.build().body(body) {
|
.and_then(|body| { // <- we got complete body, now send as server response
|
||||||
Ok(resp) => ok(resp),
|
httpcodes::HttpOk.build()
|
||||||
Err(e) => err(e.into()),
|
.body(body)
|
||||||
}
|
.map_err(error::Error::from)
|
||||||
}))
|
}))
|
||||||
},
|
.responder()
|
||||||
Err(e) => {
|
}
|
||||||
Either::B(err(error::Error::from(e)))
|
|
||||||
}
|
/// stream client request to a server response
|
||||||
|
fn streaming(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
|
// send client request
|
||||||
|
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
|
||||||
|
.finish().unwrap()
|
||||||
|
.send() // <- connect to host and send request
|
||||||
|
.map_err(error::Error::from) // <- convert SendRequestError to an Error
|
||||||
|
.and_then(|resp| { // <- we received client response
|
||||||
|
httpcodes::HttpOk.build()
|
||||||
|
// read one chunk from client response and send this chunk to a server response
|
||||||
|
// .from_err() converts PayloadError to a Error
|
||||||
|
.body(Body::Streaming(Box::new(resp.from_err())))
|
||||||
|
.map_err(|e| e.into()) // HttpOk::build() mayb return HttpError, we need to convert it to a Error
|
||||||
})
|
})
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
let _ = env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("ws-example");
|
let sys = actix::System::new("http-proxy");
|
||||||
|
|
||||||
let _addr = HttpServer::new(
|
let _addr = HttpServer::new(
|
||||||
|| Application::new()
|
|| Application::new()
|
||||||
// enable logger
|
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
|
.resource("/streaming", |r| r.f(streaming))
|
||||||
.resource("/", |r| r.f(index)))
|
.resource("/", |r| r.f(index)))
|
||||||
.bind("127.0.0.1:8080").unwrap()
|
.bind("127.0.0.1:8080").unwrap()
|
||||||
.start();
|
.start();
|
||||||
|
@ -12,3 +12,20 @@ pub use self::response::ClientResponse;
|
|||||||
pub use self::connector::{Connect, Connection, ClientConnector, ClientConnectorError};
|
pub use self::connector::{Connect, Connection, ClientConnector, ClientConnectorError};
|
||||||
pub(crate) use self::writer::HttpClientWriter;
|
pub(crate) use self::writer::HttpClientWriter;
|
||||||
pub(crate) use self::parser::{HttpResponseParser, HttpResponseParserError};
|
pub(crate) use self::parser::{HttpResponseParser, HttpResponseParserError};
|
||||||
|
|
||||||
|
|
||||||
|
use httpcodes;
|
||||||
|
use httpresponse::HttpResponse;
|
||||||
|
use error::ResponseError;
|
||||||
|
|
||||||
|
|
||||||
|
/// Convert `SendRequestError` to a `HttpResponse`
|
||||||
|
impl ResponseError for SendRequestError {
|
||||||
|
|
||||||
|
fn error_response(&self) -> HttpResponse {
|
||||||
|
match *self {
|
||||||
|
SendRequestError::Connector(_) => httpcodes::HttpBadGateway.into(),
|
||||||
|
_ => httpcodes::HttpInternalServerError.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user