Skip to content

Commit d77dc95

Browse files
committed
chore: improve reqwest error logs
1 parent 9db0b1e commit d77dc95

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/errors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) enum BeamConnectError {
1010
ProxyTimeoutError,
1111
#[error("Proxy rejected our authorization")]
1212
ProxyRejectedAuthorization,
13-
#[error("Unable to communicate with Proxy: {0}")]
13+
#[error("Unable to communicate with Proxy: {0:?}")]
1414
ProxyReqwestError(reqwest::Error),
1515
#[error("Unable to communicate with Proxy: {0}")]
1616
ProxyOtherError(String),
@@ -20,8 +20,10 @@ pub(crate) enum BeamConnectError {
2020
SerdeError(#[from] serde_json::Error),
2121
#[error("AppId {0} is not authorized to access URL {1}")]
2222
IdNotAuthorizedToAccessUrl(AppOrProxyId, Uri),
23-
#[error("Unable to communicate with target host: {0}")]
24-
CommunicationWithTargetFailed(String),
23+
#[error("Unable to communicate with target host: {0:?}")]
24+
CommunicationWithTargetFailed(reqwest::Error),
25+
#[error("Lookup of local target {0} failed")]
26+
NoLocalMapping(hyper::http::uri::Authority),
2527
#[error("Unable to fetch reply from target host: {0}")]
2628
FailedToReadTargetsReply(reqwest::Error),
2729
#[error("Response was not valid UTF-8: {0}")]

src/logic_reply.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,13 @@ async fn send_reply(task: &TaskRequest<HttpRequest>, config: &Config, resp: Resu
116116
}
117117
}
118118

119-
// TODO: Take ownership of `task` to save clones
120119
async fn execute_http_task(task: &TaskRequest<HttpRequest>, config: &Config) -> Result<Response, BeamConnectError> {
121120
let task_req = &task.body;
122-
let span = Span::current();
123-
span.record("method", field::display(&task_req.method));
124-
span.record("orig_url", field::display(&task_req.url));
121+
let vhost = task_req.url.authority().expect("Url has an authority");
125122
let target = config
126123
.targets_local
127-
.get(task_req.url.authority().unwrap()) //TODO unwrap
128-
.ok_or_else(|| {
129-
warn!("Lookup of local target {} failed", task_req.url.authority().unwrap());
130-
BeamConnectError::CommunicationWithTargetFailed(String::from("Target not defined"))
131-
})?;
124+
.get(vhost)
125+
.ok_or_else(|| BeamConnectError::NoLocalMapping(vhost.clone()))?;
132126
match &task.from {
133127
AppOrProxyId::App(app) if target.can_be_accessed_by(app) => {},
134128
id => return Err(BeamConnectError::IdNotAuthorizedToAccessUrl(id.clone(), task_req.url.clone())),
@@ -155,7 +149,7 @@ async fn execute_http_task(task: &TaskRequest<HttpRequest>, config: &Config) ->
155149
.authority(target.replace.authority.to_owned())
156150
.build()?;
157151

158-
span.record("dst_url", field::display(&uri));
152+
Span::current().record("dst_url", field::display(&uri));
159153
let mut headers = task_req.headers.clone();
160154
if target.reset_host {
161155
// This will lead to reqwest generating a new HOST header coresponding to the new hostname of the url.
@@ -171,7 +165,7 @@ async fn execute_http_task(task: &TaskRequest<HttpRequest>, config: &Config) ->
171165
.body(task_req.body.to_vec())
172166
.send()
173167
.await
174-
.map_err(|e| BeamConnectError::CommunicationWithTargetFailed(e.to_string()))?;
168+
.map_err(BeamConnectError::CommunicationWithTargetFailed)?;
175169
Ok(resp)
176170
}
177171

0 commit comments

Comments
 (0)