3

I created a server with actix_web that will connect through GET to another service using actix client and return body on success or error on error. I have been able to return the body but have no clue about how to return the error.

This is my handler:

fn echo_client(client: web::Data<Client>) -> impl Future<Item = HttpResponse, Error = Error> {
    client
        .get("127.0.0.1:9596/echo/javier") // <- Create request builder
        .header("User-Agent", "Actix-web")
        //.finish().unwrap()
        .send() // <- Send http request
        .map_err(|_| ())
        //.map_err(Error::from)
        .and_then(|response| {
            response
                .body()
                .and_then(|body| {
                    println!("{:?}", body);
                    Ok(HttpResponse::Ok().body(body))
                })
                .map_err(|error| Err(error.error_response()))
        })
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
antonof
  • 109
  • 1
  • 8

1 Answers1

2

There are three things that may fail:

  1. Failing connection.
  2. Non 200-status code.
  3. Abrupt stop in body stream.

To handle 1, do not map_err to ():

.map_err(|err| match err {
    SendRequestError::Connect(error) => {
        ErrorBadGateway(format!("Unable to connect to httpbin: {}", error))
    }
    error => ErrorInternalServerError(error),
})

SendRequestError lists the errors that can occur when doing client requests.

To handle 2, make sure you use the status code from the client response:

.and_then(|response| Ok(HttpResponse::build(response.status()).streaming(response))))

actix-web handles 3 I believe.

Complete example, handling headers too:

use actix_web::client::{Client, SendRequestError};
use actix_web::error::{ErrorBadGateway, ErrorInternalServerError};
use actix_web::{web, App, Error, HttpResponse, HttpServer};
use futures::future::Future;

fn main() {
    HttpServer::new(|| App::new().data(Client::new()).route("/", web::to(handler)))
        .bind("127.0.0.1:8000")
        .expect("Cannot bind to port 8000")
        .run()
        .expect("Unable to run server");
}

fn handler(client: web::Data<Client>) -> Box<Future<Item = HttpResponse, Error = Error>> {
    Box::new(
        client
            .get("https://httpbin.org/get")
            .no_decompress()
            .send()
            .map_err(|err| match err {
                SendRequestError::Connect(error) => {
                    ErrorBadGateway(format!("Unable to connect to httpbin: {}", error))
                }
                error => ErrorInternalServerError(error),
            })
            .and_then(|response| {
                let mut result = HttpResponse::build(response.status());
                let headers = response
                    .headers()
                    .iter()
                    .filter(|(h, _)| *h != "connection" && *h != "content-length");
                for (header_name, header_value) in headers {
                    result.header(header_name.clone(), header_value.clone());
                }
                Ok(result.streaming(response))
            }),
    )
}

Which actually failed:

$ curl -v localhost:8000
* Rebuilt URL to: localhost:8000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 502 Bad Gateway
< content-length: 50
< content-type: text/plain
< date: Sun, 07 Jul 2019 21:01:39 GMT
< 
* Connection #0 to host localhost left intact
Unable to connect to httpbin: SSL is not supported

Add ssl as a feature in Cargo.toml to fix the connection error:

actix-web = { version = "1.0", features=["ssl"] }

Then try the request again:

$ curl -v localhost:8000
* Rebuilt URL to: localhost:8000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< x-frame-options: DENY
< date: Sun, 07 Jul 2019 21:07:18 GMT
< content-type: application/json
< access-control-allow-origin: *
< access-control-allow-credentials: true
< server: nginx
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< referrer-policy: no-referrer-when-downgrade
< 
{
  "args": {}, 
  "headers": {
    "Date": "Sun, 07 Jul 2019 21:07:18 GMT", 
    "Host": "httpbin.org"
  }, 
  "origin": "212.251.175.90, 212.251.175.90", 
  "url": "https://httpbin.org/get"
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
arve0
  • 3,424
  • 26
  • 33