From 2d4121f412f8f383a95eac25475604e2510e77c7 Mon Sep 17 00:00:00 2001 From: 0x009922 <43530070+0x009922@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:03:19 +0900 Subject: [PATCH] fix(torii): display more error details (#4973) Signed-off-by: 0x009922 <43530070+0x009922@users.noreply.github.com> --- Cargo.lock | 21 +++++++++++++++++++++ core/src/tx.rs | 2 +- torii/Cargo.toml | 4 ++++ torii/src/lib.rs | 28 ++++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 31c4af879ff..4193c4bd693 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3528,6 +3528,7 @@ dependencies = [ "error-stack", "eyre", "futures", + "http-body-util", "iroha_config", "iroha_core", "iroha_data_model", @@ -3543,6 +3544,7 @@ dependencies = [ "nonzero_ext", "parity-scale-codec", "pprof", + "pretty-error-debug", "serde", "serde_json", "thiserror", @@ -4613,6 +4615,25 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "pretty-error-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a8f3888f1de6a9d977610972eab0014b663a3907ec153d77200252ad22e4bb0" +dependencies = [ + "pretty-error-debug-derive", +] + +[[package]] +name = "pretty-error-debug-derive" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "788992637e9c73f809f7bdc647572785efb06cb7c860105a4e55e9c7d6935d39" +dependencies = [ + "quote", + "syn 2.0.72", +] + [[package]] name = "proc-macro-crate" version = "3.1.0" diff --git a/core/src/tx.rs b/core/src/tx.rs index ead5ce71213..1218707ff4b 100644 --- a/core/src/tx.rs +++ b/core/src/tx.rs @@ -56,7 +56,7 @@ pub enum AcceptTransactionFail { SignatureVerification(#[source] SignatureVerificationFail), /// The genesis account can only sign transactions in the genesis block UnexpectedGenesisAccountSignature, - /// Chain id doesn't correspond to the id of current blockchain + /// Chain id doesn't correspond to the id of current blockchain: {0} ChainIdMismatch(Mismatch), } diff --git a/torii/Cargo.toml b/torii/Cargo.toml index 662a37d2725..7a541e7fb86 100644 --- a/torii/Cargo.toml +++ b/torii/Cargo.toml @@ -54,3 +54,7 @@ parity-scale-codec = { workspace = true, features = ["derive"] } # TODO: switch to original crate once fix is merged (https://github.com/tikv/pprof-rs/pull/241) pprof = { git = " https://github.com/Erigara/pprof-rs", branch = "fix_pointer_align", optional = true, default-features = false, features = ["protobuf-codec", "frame-pointer", "cpp"] } nonzero_ext = { workspace = true } +pretty-error-debug = "0.3.0" + +[dev-dependencies] +http-body-util = "0.1.2" diff --git a/torii/src/lib.rs b/torii/src/lib.rs index ecc72d3d1c5..a6a0f83d361 100644 --- a/torii/src/lib.rs +++ b/torii/src/lib.rs @@ -314,7 +314,7 @@ impl Torii { } /// Torii errors. -#[derive(Debug, thiserror::Error, displaydoc::Display)] +#[derive(thiserror::Error, displaydoc::Display, pretty_error_debug::Debug)] pub enum Error { /// Failed to process query Query(#[from] iroha_data_model::ValidationFail), @@ -343,7 +343,7 @@ impl IntoResponse for Error { fn into_response(self) -> Response { match self { Self::Query(err) => (Self::query_status_code(&err), utils::Scale(err)).into_response(), - _ => (self.status_code(), self.to_string()).into_response(), + _ => (self.status_code(), format!("{self:?}")).into_response(), } } } @@ -400,3 +400,27 @@ impl Error { /// Result type pub type Result = std::result::Result; + +#[cfg(test)] +mod tests { + // for `collect` + use http_body_util::BodyExt as _; + + use super::*; + + #[tokio::test] + async fn error_response_contains_details() { + let err = Error::AcceptTransaction(iroha_core::tx::AcceptTransactionFail::ChainIdMismatch( + iroha_data_model::isi::error::Mismatch { + expected: "123".try_into().unwrap(), + actual: "321".try_into().unwrap(), + }, + )); + let response = err.into_response(); + + let body = response.into_body().collect().await.unwrap().to_bytes(); + let text = String::from_utf8(body.iter().map(|x| *x).collect()) + .expect("to be a valid UTF8 string"); + assert_eq!(text, "Failed to accept transaction\n\nCaused by:\n Chain id doesn't correspond to the id of current blockchain: Expected ChainId(\"123\"), actual ChainId(\"321\")"); + } +}