From b19eb796643d71b9d7c0906f9685e3c705b71296 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 12 Dec 2024 18:25:36 +0700 Subject: [PATCH] feat(blockchain-tree-api): remove manual implementations of core::error::Error (#13332) --- crates/blockchain-tree-api/src/error.rs | 37 ++++--------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/crates/blockchain-tree-api/src/error.rs b/crates/blockchain-tree-api/src/error.rs index 92866b4d4dad..9c20f4baaccb 100644 --- a/crates/blockchain-tree-api/src/error.rs +++ b/crates/blockchain-tree-api/src/error.rs @@ -166,42 +166,17 @@ impl std::fmt::Debug for InsertBlockError { } } +#[derive(thiserror::Error, Debug)] +#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {kind}", + .block.hash(), + .block.number, + .block.parent_hash)] struct InsertBlockErrorData { block: SealedBlock, + #[source] kind: InsertBlockErrorKind, } -impl std::fmt::Display for InsertBlockErrorData { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Failed to insert block (hash={}, number={}, parent_hash={}): {}", - self.block.hash(), - self.block.number, - self.block.parent_hash, - self.kind - ) - } -} - -impl std::fmt::Debug for InsertBlockErrorData { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("InsertBlockError") - .field("error", &self.kind) - .field("hash", &self.block.hash()) - .field("number", &self.block.number) - .field("parent_hash", &self.block.parent_hash) - .field("num_txs", &self.block.body.transactions.len()) - .finish_non_exhaustive() - } -} - -impl core::error::Error for InsertBlockErrorData { - fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { - Some(&self.kind) - } -} - impl InsertBlockErrorData { const fn new(block: SealedBlock, kind: InsertBlockErrorKind) -> Self { Self { block, kind }