Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move BoxError downcast out of impl From<BoxError> for TransactionError #5732

Closed
arya2 opened this issue Nov 28, 2022 · 5 comments
Closed

Move BoxError downcast out of impl From<BoxError> for TransactionError #5732

arya2 opened this issue Nov 28, 2022 · 5 comments
Labels
C-enhancement Category: This is an improvement S-needs-investigation Status: Needs further investigation S-needs-triage Status: A bug report needs triage

Comments

@arya2
Copy link
Contributor

arya2 commented Nov 28, 2022

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

@arya2 arya2 added C-enhancement Category: This is an improvement S-needs-triage Status: A bug report needs triage labels Nov 28, 2022
@teor2345 teor2345 added the S-needs-investigation Status: Needs further investigation label Nov 28, 2022
@teor2345
Copy link
Contributor

These downcasts are located in the same place, so we don't accidentally implement conflicting downcasts. Moving the downcasts out of the conversion could make the code harder to maintain, and create more bugs.

It would be better for the compiler to complain when there isn't a valid conversion for a particular type of error and to perform the conversions where the type of BoxError is more explicitly known.

Unfortunately, it is impossible to know the type of many errors, because tower::Buffer converts all service readiness and response errors to BoxError:
https://docs.rs/tower/latest/tower/buffer/struct.Buffer.html#impl-Service%3CRequest%3E

Before we can prioritise or estimate this ticket, it will need a specific design that takes these issues into account.

@arya2
Copy link
Contributor Author

arya2 commented Nov 29, 2022

Unfortunately, it is impossible to know the type of many errors, because tower::Buffer converts all service readiness and response errors to BoxError

Ah, I see. Still, it might be a good idea to do the conversion outside a From impl so it's more obvious where the conversions are happening. I added a possible design.

@teor2345
Copy link
Contributor

It would be better for the compiler to complain when there isn't a valid conversion for a particular type of error

I don't think the design in the ticket does this?

@arya2
Copy link
Contributor Author

arya2 commented Nov 30, 2022

It would be better for the compiler to complain when there isn't a valid conversion for a particular type of error

I don't think the design in the ticket does this?

My wording there may have been a bit vague, it's more to disallow using ? or into() without explicitly defining how the BoxError should be converted into a TransactionError so it's more obvious when a new variant needs to be added to TransactionError.

It might also be nice to annotate where each type is expected.

@teor2345
Copy link
Contributor

teor2345 commented Dec 1, 2022

It might also be nice to annotate where each type is expected.

I think we can use concrete types, rather than comments, so the compiler can check them.

I've added this as a TODO in the code, so we do it when we're rewriting that error handling.

@mpguerra mpguerra added this to Zebra Jan 24, 2023
@mpguerra mpguerra moved this to 🆕 New in Zebra Jan 24, 2023
@mpguerra mpguerra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2024
@github-project-automation github-project-automation bot moved this from New to Done in Zebra Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: This is an improvement S-needs-investigation Status: Needs further investigation S-needs-triage Status: A bug report needs triage
Projects
Archived in project
Development

No branches or pull requests

3 participants