如何从客户端处理程序向调用者返回错误

时间:2019-04-15 14:14:37

标签: rust actix-web

我用actix_web创建了一个服务器,该服务器将使用actix客户端通过GET连接到另一服务,并在成功或错误时返回主体。我已经能够退回尸体,但不知道阿博托如何退回错误。

我已经尝试过所有想到但无能为力的事情。...

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())
                    })
                })
}

这是我做的处理程序。

1 个答案:

答案 0 :(得分:0)

三件事可能会失败:

  1. 连接失败。
  2. 非200状态代码。
  3. 突然停止在体内。

要处理1,请对map_err ()

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

SendRequestError列出了执行客户端请求时可能发生的错误。

要处理2,请确保使用客户端响应中的状态代码:

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

actix-web可以处理3个。

完整示例,也处理标头:

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))
            }),
    )
}

实际上失败了:

$ 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

将ssl添加为Cargo.toml中的功能以修复连接错误:

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

然后重试该请求:

$ 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"
}