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

@@ -19,6 +19,7 @@ use self::types::{Error as ResponseError, *};
use futures::Stream;
use futures_util::StreamExt;
use lighthouse_network::PeerId;
use pretty_reqwest_error::PrettyReqwestError;
pub use reqwest;
use reqwest::{IntoUrl, RequestBuilder, Response};
pub use reqwest::{StatusCode, Url};
@@ -39,7 +40,7 @@ pub const CONSENSUS_VERSION_HEADER: &str = "Eth-Consensus-Version";
#[derive(Debug)]
pub enum Error {
/// The `reqwest` client raised an error.
Reqwest(reqwest::Error),
HttpClient(PrettyReqwestError),
/// The server returned an error message where the body was able to be parsed.
ServerMessage(ErrorMessage),
/// The server returned an error message with an array of errors.
@@ -70,7 +71,7 @@ pub enum Error {
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Error::Reqwest(error)
Error::HttpClient(error.into())
}
}
@@ -78,7 +79,7 @@ impl Error {
/// If the error has a HTTP status code, return it.
pub fn status(&self) -> Option<StatusCode> {
match self {
Error::Reqwest(error) => error.status(),
Error::HttpClient(error) => error.inner().status(),
Error::ServerMessage(msg) => StatusCode::try_from(msg.code).ok(),
Error::ServerIndexedMessage(msg) => StatusCode::try_from(msg.code).ok(),
Error::StatusCode(status) => Some(*status),
@@ -278,7 +279,7 @@ impl BeaconNodeHttpClient {
.await?
.json()
.await
.map_err(Error::Reqwest)
.map_err(Into::into)
}
/// Perform a HTTP POST request with a custom timeout.
@@ -303,7 +304,7 @@ impl BeaconNodeHttpClient {
.await?
.json()
.await
.map_err(Error::Reqwest)
.map_err(Error::from)
}
/// Generic POST function supporting arbitrary responses and timeouts.
@@ -1645,7 +1646,7 @@ impl BeaconNodeHttpClient {
.bytes_stream()
.map(|next| match next {
Ok(bytes) => EventKind::from_sse_bytes(bytes.as_ref()),
Err(e) => Err(Error::Reqwest(e)),
Err(e) => Err(Error::HttpClient(e.into())),
}))
}

View File

@@ -364,12 +364,12 @@ pub struct DatabaseInfo {
impl BeaconNodeHttpClient {
/// Perform a HTTP GET request, returning `None` on a 404 error.
async fn get_bytes_opt<U: IntoUrl>(&self, url: U) -> Result<Option<Vec<u8>>, Error> {
let response = self.client.get(url).send().await.map_err(Error::Reqwest)?;
let response = self.client.get(url).send().await.map_err(Error::from)?;
match ok_or_error(response).await {
Ok(resp) => Ok(Some(
resp.bytes()
.await
.map_err(Error::Reqwest)?
.map_err(Error::from)?
.into_iter()
.collect::<Vec<_>>(),
)),

View File

@@ -170,7 +170,7 @@ impl ValidatorClientHttpClient {
.map_err(|_| Error::InvalidSignatureHeader)?
.to_string();
let body = response.bytes().await.map_err(Error::Reqwest)?;
let body = response.bytes().await.map_err(Error::from)?;
let message =
Message::parse_slice(digest(&SHA256, &body).as_ref()).expect("sha256 is 32 bytes");
@@ -222,7 +222,7 @@ impl ValidatorClientHttpClient {
.headers(self.headers()?)
.send()
.await
.map_err(Error::Reqwest)?;
.map_err(Error::from)?;
ok_or_error(response).await
}
@@ -236,7 +236,7 @@ impl ValidatorClientHttpClient {
.await?
.json()
.await
.map_err(Error::Reqwest)
.map_err(Error::from)
}
/// Perform a HTTP GET request, returning `None` on a 404 error.
@@ -266,7 +266,7 @@ impl ValidatorClientHttpClient {
.json(body)
.send()
.await
.map_err(Error::Reqwest)?;
.map_err(Error::from)?;
ok_or_error(response).await
}
@@ -297,7 +297,7 @@ impl ValidatorClientHttpClient {
.json(body)
.send()
.await
.map_err(Error::Reqwest)?;
.map_err(Error::from)?;
let response = ok_or_error(response).await?;
self.signed_body(response).await?;
Ok(())
@@ -316,7 +316,7 @@ impl ValidatorClientHttpClient {
.json(body)
.send()
.await
.map_err(Error::Reqwest)?;
.map_err(Error::from)?;
ok_or_error(response).await
}