Skip to content

Commit

Permalink
fix(torii): display more error details (#4973)
Browse files Browse the repository at this point in the history
Signed-off-by: 0x009922 <[email protected]>
  • Loading branch information
0x009922 authored Aug 21, 2024
1 parent ebe6515 commit 2d4121f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChainId>),
}

Expand Down
4 changes: 4 additions & 0 deletions torii/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
28 changes: 26 additions & 2 deletions torii/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -400,3 +400,27 @@ impl Error {

/// Result type
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[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\")");
}
}

0 comments on commit 2d4121f

Please sign in to comment.