diff --git a/src/utils/exceptions.rs b/src/utils/exceptions.rs index 7efc3031..9b19d4e6 100644 --- a/src/utils/exceptions.rs +++ b/src/utils/exceptions.rs @@ -39,8 +39,8 @@ pub enum XRPLUtilsException { FromHexError(#[from] hex::FromHexError), #[error("ParseInt error: {0}")] ParseIntError(#[from] core::num::ParseIntError), - #[error("Invalid UTF-8")] - Utf8Error, + #[error("Invalid UTF-8: {0}")] + Utf8Error(#[from] core::str::Utf8Error), } #[derive(Debug, Clone, PartialEq, Error)] @@ -119,12 +119,6 @@ pub enum ISOCodeException { UnsupportedCurrencyRepresentation, } -impl From for XRPLUtilsException { - fn from(_: core::str::Utf8Error) -> Self { - XRPLUtilsException::Utf8Error - } -} - impl From for XRPLUtilsException { fn from(error: serde_json::Error) -> Self { XRPLUtilsException::SerdeJsonError(error.into()) diff --git a/src/utils/str_conversion.rs b/src/utils/str_conversion.rs index 42dd6333..84acfabb 100644 --- a/src/utils/str_conversion.rs +++ b/src/utils/str_conversion.rs @@ -63,4 +63,16 @@ mod tests { let result = hex_to_str(Cow::Borrowed("ff")); assert!(result.is_err()); } + + #[test] + fn test_hex_to_str_invalid_utf8_preserves_detail() { + use crate::utils::exceptions::XRPLUtilsException; + + // "41ff" decodes to [0x41, 0xff]: 'A' is valid, 0xff is not, so the + // Utf8Error should report `valid_up_to() == 1`. + match hex_to_str(Cow::Borrowed("41ff")).unwrap_err() { + XRPLUtilsException::Utf8Error(error) => assert_eq!(error.valid_up_to(), 1), + other => panic!("expected Utf8Error, got {other:?}"), + } + } }