From 14bf3a15b0c806849845e5822e708057e3c20c78 Mon Sep 17 00:00:00 2001 From: Arun Kulshreshtha Date: Mon, 20 May 2019 22:55:53 -0700 Subject: [PATCH] edenapi: check HTTP status code Summary: Previously, `edenapi` would unconditionally attempt to parse the response body as CBOR, resulting in cryptic error messages if the server had returned an error instead of the expected content. Instead, we should first check that the request was successful, and only then attempt to parse the body. Reviewed By: quark-zju Differential Revision: D15426868 fbshipit-source-id: ceaf0d6691b509bcd934d6b99ceefb3e83e798ca --- lib/edenapi/src/curl.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/edenapi/src/curl.rs b/lib/edenapi/src/curl.rs index 01feb6a308..a1c2608fcb 100644 --- a/lib/edenapi/src/curl.rs +++ b/lib/edenapi/src/curl.rs @@ -247,8 +247,19 @@ impl EdenApiCurlClient { let start = Instant::now(); driver.perform(|res| { - let easy = res?; + let mut easy = res?; + let code = easy.response_code()?; let data = easy.get_ref().data(); + + if code >= 400 { + let msg = String::from_utf8_lossy(data); + bail!( + "Received HTTP status code {} with response: {:?}", + code, + msg + ); + } + let response = serde_cbor::from_slice::(data)?; response_cb(response) })?;