zebra icon indicating copy to clipboard operation
zebra copied to clipboard

Move BoxError downcast out of `impl From<BoxError> for TransactionError`

Open arya2 opened this issue 1 year ago • 5 comments

Motivation

It would be better for the compiler to complain about a mismatch between BoxError / TransactionError when using the ? operator, and to perform the conversions where the type of BoxError is more explicitly known.

TransactionError is currently the only error type with a From impl handling downcasts.

This could be used in the block verifier for VerifyBlockError::ValidateProposal as well if defined in a trait.

Possible Design

impl TransactionError {
    pub(crate) fn downcast_from<ErrorType>(mut err: BoxError) -> Self
    where
        ErrorType: std::error::Error + Into<TransactionError>,
    {
        err.downcast::<ErrorType>()
            .map(|e| Self::from(*e))
            .or_else(Self::internal_downcast_err)
    }
}

Related Work

  • #1186

arya2 avatar Nov 28 '22 18:11 arya2