Skip to content

Commit

Permalink
chore: improve hyper http error messages (alloy-rs#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored and ben186 committed Jul 27, 2024
1 parent 7fa06e3 commit 9709a65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 4 additions & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ mod tests {
use crate::transaction::SignableTransaction;
use alloy_eips::eip2930::{AccessList, AccessListItem};
use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256};
use std::{fs, path::PathBuf, vec, vec::Vec};
use std::{fs, path::PathBuf, vec};

#[cfg(not(feature = "std"))]
use std::vec::Vec;

#[test]
#[cfg(feature = "k256")]
Expand Down
10 changes: 8 additions & 2 deletions crates/serde/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ pub mod u128_hex_or_decimal_opt {

/// serde functions for handling `Vec<u128>` as [U128](alloy_primitives::U128)
pub mod u128_hex_or_decimal_vec {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use alloy_primitives::U128;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or
/// a number
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u128>, D::Error>
Expand All @@ -314,10 +316,12 @@ pub mod u128_hex_or_decimal_vec {

/// serde functions for handling `Vec<Vec<u128>>` as [U128](alloy_primitives::U128)
pub mod u128_hex_or_decimal_vec_vec_opt {
use alloc::vec::Vec;
use alloy_primitives::U128;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Deserializes an `u128` accepting a hex quantity string with optional 0x prefix or
/// a number
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<Vec<u128>>>, D::Error>
Expand Down Expand Up @@ -353,9 +357,11 @@ pub mod u128_hex_or_decimal_vec_vec_opt {
#[cfg(test)]
mod tests {
use super::*;
use alloc::{vec, vec::Vec};
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

#[test]
fn test_hex_u64() {
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down
9 changes: 4 additions & 5 deletions crates/transport-http/src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ where

if status != hyper::StatusCode::OK {
return Err(TransportErrorKind::custom_str(&format!(
r#"HTTP error: {} with body: "{}""#,
status,
String::from_utf8_lossy(body.as_ref())
"HTTP error {status} with body: {}",
String::from_utf8_lossy(&body)
)));
}

// Deser a Box<RawValue> from the body. If deser fails, return the
// body as a string in the error. If the body is not UTF8, this will
// fail and give the empty string in the error.
serde_json::from_slice(body.as_ref()).map_err(|err| {
TransportError::deser_err(err, std::str::from_utf8(body.as_ref()).unwrap_or(""))
serde_json::from_slice(&body).map_err(|err| {
TransportError::deser_err(err, String::from_utf8_lossy(body.as_ref()))
})
})
}
Expand Down

0 comments on commit 9709a65

Please sign in to comment.