Tidy formatting of Reqwest errors (#4336)

## Issue Addressed

NA

## Proposed Changes

Implements the `PrettyReqwestError` to wrap a `reqwest::Error` and give nicer `Debug` formatting. It also wraps the `Url` component in a `SensitiveUrl` to avoid leaking sensitive info in logs.

### Before

```
Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(9999), path: "/eth/v1/node/version", query: None, fragment: None }, source: hyper::Error(Connect, ConnectError("tcp connect error", Os { code: 61, kind: ConnectionRefused, message: "Connection refused" })) })
```

### After

```
HttpClient(url: http://localhost:9999/, kind: request, detail: error trying to connect: tcp connect error: Connection refused (os error 61))
```

## Additional Info

I've also renamed the `Reqwest` error enum variants to `HttpClient`, to give people a better chance at knowing what's going on. Reqwest is pretty odd and looks like a typo.

I've implemented it in the `eth2` and `execution_layer` crates. This should affect most logs in the VC and EE-related ones in the BN.

I think the last crate that could benefit from the is the `beacon_node/eth1` crate. I haven't updated it in this PR since its error type is not so amenable to it (everything goes into a `String`). I don't have a whole lot of time to jig around with that at the moment and I feel that this PR as it stands is a significant enough improvement to merge on its own. Leaving it as-is is fine for the time being and we can always come back for it later (or implement in-protocol deposits!).
This commit is contained in:
Paul Hauner
2023-06-27 01:06:50 +00:00
parent 448d3ec9b3
commit 9072acbfa6
12 changed files with 113 additions and 20 deletions

View File

@@ -0,0 +1,10 @@
[package]
name = "pretty_reqwest_error"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11.0", features = ["json","stream"] }
sensitive_url = { path = "../sensitive_url" }

View File

@@ -0,0 +1,62 @@
use sensitive_url::SensitiveUrl;
use std::error::Error as StdError;
use std::fmt;
pub struct PrettyReqwestError(reqwest::Error);
impl PrettyReqwestError {
pub fn inner(&self) -> &reqwest::Error {
&self.0
}
}
impl fmt::Debug for PrettyReqwestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(url) = self.0.url() {
if let Ok(url) = SensitiveUrl::new(url.clone()) {
write!(f, "url: {}", url)?;
} else {
write!(f, "url: unable_to_parse")?;
};
}
let kind = if self.0.is_builder() {
"builder"
} else if self.0.is_redirect() {
"redirect"
} else if self.0.is_status() {
"status"
} else if self.0.is_timeout() {
"timeout"
} else if self.0.is_request() {
"request"
} else if self.0.is_connect() {
"connect"
} else if self.0.is_body() {
"body"
} else if self.0.is_decode() {
"decode"
} else {
"unknown"
};
write!(f, ", kind: {}", kind)?;
if let Some(status) = self.0.status() {
write!(f, ", status_code: {}", status)?;
}
if let Some(ref source) = self.0.source() {
write!(f, ", detail: {}", source)?;
} else {
write!(f, ", source: unknown")?;
}
Ok(())
}
}
impl From<reqwest::Error> for PrettyReqwestError {
fn from(inner: reqwest::Error) -> Self {
Self(inner)
}
}