Skip to content

Commit b1f698c

Browse files
authored
Improve http client error handling (#459)
* Improve http client error handling Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Fix agency error details propagation, refactor response mocking code Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Fix codecov hash in CI Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
1 parent d95225a commit b1f698c

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ jobs:
111111
POOL_DOCKERFILE_HASH=${{ hashFiles('ci/indy-pool.dockerfile')}}
112112
113113
LIBVCX_HASH=${LIBVCX_SOURCE_HASH:0:11}-${ARIESVCX_SOURCE_HASH:0:11}-${LIBVCX_NODE_WRAPPERS_HASH:0:11}-${LIBVCX_NODE_AGENT_HASH:0:11}-${LIBVCX_DOCKERFILE_HASH:0:11}-${AGENCY_CLIENT_HASH:0:11}
114-
CODECOV_HASH=${LIBVCX_SOURCE_HASH:0:23}-${ARIESVCX_SOURCE_HASH:0:11}-${CODECOV_DOCKERFILE_HASH:0:23}
114+
CODECOV_HASH=${LIBVCX_SOURCE_HASH:0:23}-${ARIESVCX_SOURCE_HASH:0:11}-${AGENCY_CLIENT_HASH:0:11}-${CODECOV_DOCKERFILE_HASH:0:23}
115115
ANDROID_HASH=${LIBVCX_SOURCE_HASH:0:15}-${ARIESVCX_SOURCE_HASH:0:11}-${JAVA_WRAPPERS_HASH:0:15}
116116
POOL_HASH=${POOL_DOCKERFILE_HASH:0:15}
117117

agency_client/src/httpclient.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::env;
2+
use std::time::Duration;
23

4+
use async_std::sync::RwLock;
35
use reqwest;
4-
use reqwest::header::CONTENT_TYPE;
56
use reqwest::Client;
6-
use async_std::sync::RwLock;
7-
use std::time::Duration;
7+
use reqwest::header::CONTENT_TYPE;
88

9+
use crate::{AgencyMockDecrypted, mocking};
910
use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
10-
use crate::mocking::{AgencyMock, AgencyMockDecrypted, HttpClientMockResponse};
11+
use crate::mocking::{AgencyMock, HttpClientMockResponse};
1112
use crate::utils::timeout::TimeoutUtils;
12-
use crate::mocking;
1313

1414
lazy_static! {
1515
static ref HTTP_CLIENT: RwLock<Client> = RwLock::new(reqwest::ClientBuilder::new()
@@ -22,18 +22,17 @@ lazy_static! {
2222
}
2323

2424
pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResult<Vec<u8>> {
25-
// todo: this function should be general, not knowing that agency exists -> move agency mocks to agency module
2625
if mocking::agency_mocks_enabled() {
2726
if HttpClientMockResponse::has_response() {
28-
warn!("HttpClient has mocked response");
27+
warn!("post_to_agency >> mocking response for POST {}", url);
2928
return HttpClientMockResponse::get_response();
3029
}
3130
if AgencyMockDecrypted::has_decrypted_mock_responses() {
32-
warn!("Agency requests returns empty response, decrypted mock response is available");
31+
warn!("post_to_agency >> will use mocked decrypted response for POST {}", url);
3332
return Ok(vec!());
3433
}
3534
let mocked_response = AgencyMock::get_response();
36-
debug!("Agency returns mocked response of length {}", mocked_response.len());
35+
warn!("post_to_agency >> mocking response of length {} for POST {}", mocked_response.len(), url);
3736
return Ok(mocked_response);
3837
}
3938

@@ -44,7 +43,7 @@ pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResu
4443
}
4544

4645
let client = HTTP_CLIENT.read().await;
47-
debug!("Posting encrypted bundle to: \"{}\"", url);
46+
debug!("post_to_agency >> http client sending request POST {}", url);
4847

4948
let response =
5049
client.post(url)
@@ -53,21 +52,22 @@ pub async fn post_message(body_content: &Vec<u8>, url: &str) -> AgencyClientResu
5352
.send()
5453
.await
5554
.map_err(|err| {
56-
AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("Could not connect {:?}", err))
55+
AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("HTTP Client could not connect with {}, err: {}", url, err.to_string()))
5756
})?;
5857

59-
if !response.status().is_success() {
60-
match response.text().await {
61-
Ok(content) => {
62-
Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("Agency responded with error. Details: {}", content)))
63-
}
64-
Err(_) => {
65-
Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("Agency response could not be read.")))
58+
let content_length = response.content_length();
59+
let response_status = response.status();
60+
match response.text().await {
61+
Ok(payload) => {
62+
if response_status.is_success() {
63+
Ok(payload.into_bytes())
64+
} else {
65+
Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("POST {} failed due to non-success HTTP status: {}, response body: {}", url, response_status.to_string(), payload)))
6666
}
6767
}
68-
} else {
69-
Ok(response.text().await
70-
.or(Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, "could not read response")))?.into())
68+
Err(error) => {
69+
Err(AgencyClientError::from_msg(AgencyClientErrorKind::PostMessageFailed, format!("POST {} failed because response could not be decoded as utf-8, HTTP status: {}, content-length header: {:?}, error: {:?}", url, response_status.to_string(), content_length, error)))
70+
}
7171
}
7272
}
7373

agency_client/src/utils/comm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::{agency_settings, httpclient};
2-
use crate::error::AgencyClientResult;
1+
use crate::{agency_settings, AgencyClientResult, httpclient};
32

43
pub async fn post_to_agency(body_content: &Vec<u8>) -> AgencyClientResult<Vec<u8>> {
54
let endpoint = agency_settings::get_config_value(agency_settings::CONFIG_AGENCY_ENDPOINT)?;

aries_vcx/src/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use failure::{Backtrace, Context, Fail};
55

66
use agency_client;
77

8-
use crate::utils;
98
use crate::utils::error;
109

1110
pub mod prelude {
@@ -300,7 +299,7 @@ impl From<VcxErrorKind> for VcxError {
300299
impl From<agency_client::error::AgencyClientError> for VcxError {
301300
fn from(agency_err: agency_client::error::AgencyClientError) -> VcxError {
302301
let kind_num: u32 = agency_err.kind().into();
303-
VcxError::from_msg(kind_num.into(), utils::error::error_message(&agency_err.kind().clone().into()))
302+
VcxError::from_msg(kind_num.into(), agency_err.to_string())
304303
}
305304
}
306305

0 commit comments

Comments
 (0)