From ea157013c665f12d748b0cae49c552034c2d5342 Mon Sep 17 00:00:00 2001 From: Erikson Tung Date: Fri, 8 Nov 2019 12:49:45 -0800 Subject: [PATCH] apiclient: error on non-2xx response Adds a new error type for when response code is not in 2xx range --- workspaces/api/apiclient/src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/workspaces/api/apiclient/src/lib.rs b/workspaces/api/apiclient/src/lib.rs index e7007cedddc..96e4e3d4237 100644 --- a/workspaces/api/apiclient/src/lib.rs +++ b/workspaces/api/apiclient/src/lib.rs @@ -18,7 +18,7 @@ use futures::Future; use hyper::rt::Stream; use hyper::{header, Body, Client, Request}; use hyperlocal::{UnixConnector, Uri}; -use snafu::ResultExt; +use snafu::{ensure, ResultExt}; use std::path::Path; use tokio::runtime::Runtime; @@ -38,6 +38,12 @@ mod error { #[snafu(display("Failed to send request: {}", source))] RequestSend { source: hyper::Error }, + #[snafu(display("Status {} {} when requesting {}", code.as_u16(), code.as_str(), uri))] + ResponseStatus { + code: http::StatusCode, + uri: http::uri::Uri, + }, + #[snafu(display("Failed to read body of response: {}", source))] ResponseBodyRead { source: hyper::Error }, @@ -87,7 +93,7 @@ where let request = Request::builder() .method(method.as_ref()) - .uri(uri) + .uri(&uri) .header(header::CONTENT_TYPE, "application/json") .body(Body::from(request_data)) .context(error::RequestSetup)?; @@ -102,6 +108,15 @@ where .block_on(client.request(request).map(|res| res.into_parts())) .context(error::RequestSend)?; + // Error if the response status is in not in the 2xx range. + ensure!( + head.status.is_success(), + error::ResponseStatus { + code: head.status, + uri + } + ); + // Wait on the second future (the streaming body) and concatenate all the pieces together so we // have a single response body. We make sure each piece is a string, as we go; we assume that // we're not handling binary data.