Skip to content

Commit ad96bd9

Browse files
committed
fix(chain)!: remove Debug usage and implement Display for CalculateFeeError
Limited to 3 max shown items and added a suffix when there are additional entries.
1 parent d0889f3 commit ad96bd9

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,33 @@ pub enum CalculateFeeError {
256256
impl fmt::Display for CalculateFeeError {
257257
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
258258
match self {
259-
CalculateFeeError::MissingTxOut(outpoints) => write!(
260-
f,
261-
"missing `TxOut` for one or more of the inputs of the tx: {outpoints:?}",
262-
),
263-
CalculateFeeError::NegativeFee(fee) => write!(
264-
f,
265-
"transaction is invalid according to the graph and has negative fee: {}",
266-
fee.display_dynamic()
267-
),
259+
CalculateFeeError::MissingTxOut(outpoints) => {
260+
let max_show = 3;
261+
let shown: Vec<_> = outpoints.iter().take(max_show).collect();
262+
let remaining = outpoints.len().saturating_sub(max_show);
263+
264+
write!(f, "missing `TxOut` for input(s): ")?;
265+
if outpoints.is_empty() {
266+
write!(f, "<none>")
267+
} else {
268+
write!(f, "{}", shown[0])?;
269+
for op in &shown[1..] {
270+
write!(f, ", {}", op)?;
271+
}
272+
if remaining > 0 {
273+
write!(f, " (+{} more)", remaining)?;
274+
}
275+
Ok(())
276+
}
277+
}
278+
CalculateFeeError::NegativeFee(fee) => {
279+
write!(
280+
f,
281+
"invalid transaction: negative fee {}",
282+
fee.display_dynamic()
283+
)?;
284+
Ok(())
285+
}
268286
}
269287
}
270288
}

0 commit comments

Comments
 (0)