Add missing `std::fmt::Display` implementations for error types.

This commit is contained in:
Kris Nuttycombe 2022-11-10 20:29:50 -07:00
parent d65a3fe84f
commit ed96131c4f
4 changed files with 76 additions and 0 deletions

View File

@ -189,6 +189,26 @@ pub enum GreedyInputSelectorError<ChangeStrategyErrT, NoteRefT> {
Change(ChangeError<ChangeStrategyErrT, NoteRefT>),
}
impl<CE: fmt::Display, N: fmt::Display> fmt::Display for GreedyInputSelectorError<CE, N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
GreedyInputSelectorError::Balance(e) => write!(
f,
"A balance calculation violated amount validity bounds: {:?}.",
e
),
GreedyInputSelectorError::UnsupportedAddress(_) => {
// we can't encode the UA to its string representation because we
// don't have network parameters here
write!(f, "Unified address contains no supported receivers.")
}
GreedyInputSelectorError::Change(err) => {
write!(f, "An error occurred computing change and fees: {}", err)
}
}
}
}
impl<DbErrT, ChangeStrategyErrT, NoteRefT>
From<GreedyInputSelectorError<ChangeStrategyErrT, NoteRefT>>
for InputSelectorError<DbErrT, GreedyInputSelectorError<ChangeStrategyErrT, NoteRefT>>

View File

@ -1,3 +1,5 @@
use std::fmt;
use zcash_primitives::{
consensus::{self, BlockHeight},
transaction::{
@ -95,6 +97,33 @@ pub enum ChangeError<E, NoteRefT> {
StrategyError(E),
}
impl<CE: fmt::Display, N: fmt::Display> fmt::Display for ChangeError<CE, N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
ChangeError::InsufficientFunds {
available,
required,
} => write!(
f,
"Insufficient funds: required {} ZAT, but only {} ZAT were available.",
i64::from(required),
i64::from(available)
),
ChangeError::DustInputs {
transparent,
sapling,
} => {
// we can't encode the UA to its string representation because we
// don't have network parameters here
write!(f, "Insufficient funds: {} dust inputs were present, but would cost more to spend than they are worth.", transparent.len() + sapling.len())
}
ChangeError::StrategyError(err) => {
write!(f, "{}", err)
}
}
}
}
impl<NoteRefT> From<BalanceError> for ChangeError<BalanceError, NoteRefT> {
fn from(err: BalanceError) -> ChangeError<BalanceError, NoteRefT> {
ChangeError::StrategyError(err)

View File

@ -261,6 +261,20 @@ pub enum BalanceError {
Underflow,
}
impl std::fmt::Display for BalanceError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self {
BalanceError::Overflow => {
write!(f, "Amount addition resulted in a value the valid range.")
}
BalanceError::Underflow => write!(
f,
"Amount subtraction resulted in a value outside the valid range."
),
}
}
}
#[cfg(any(test, feature = "test-dependencies"))]
pub mod testing {
use proptest::prelude::prop_compose;

View File

@ -98,6 +98,19 @@ impl From<BalanceError> for FeeError {
}
}
impl std::fmt::Display for FeeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self {
FeeError::Balance(e) => write!(
f,
"A balance calculation violated amount validity bounds: {}.",
e
),
FeeError::NonP2pkhInputs(_) => write!(f, "Only P2PKH inputs are supported."),
}
}
}
impl super::FeeRule for FeeRule {
type Error = FeeError;