Skip to content

Cannot use trait Custom: std::error::Error as boxed source due to sealed AsDynError #212

Closed
@Byron

Description

@Byron

Thanks for this crate :). I use it all the time and even switched from quick-error to it because it seemed to have no real disadvantages.

My problem is probably most concisely described with code (playground):

use thiserror; // 1.0.37

/// An error which can tell whether it's worth retrying to maybe succeed next time.
pub trait IsSpuriousError: std::error::Error {
    /// Return `true` if retrying might result in a different outcome due to IO working out differently.
    fn is_spurious(&self) -> bool {
        false
    }
}

#[derive(Debug, thiserror::Error)]
#[error("works as AsDynError is implemented for std::error::Error in various ways")]
pub struct WorksError {
    source: Box<dyn std::error::Error + Send + Sync>
}

#[derive(Debug, thiserror::Error)]
#[error("can't work since AsDynTrait isn't implemented for IsSpuriousError and can't be implemented by author either")]
pub struct FailsError {
    source: Box<dyn IsSpuriousError + Send + Sync>
}

fn main() { }

Due to the sealed and private trait AsDynError I am unable to have custom traits as Error which derive from std::error::Error. Probably there is a very good reason that I am missing and I'd be happy to learn.

The motivation for such trait is cargo which will retry if it thinks the encountered error is spurious. I thought it would be nice if the respective gitoxide errors could say so via is_spurious().

The workaround is boilerplate heavy and nearly undoes the benefit of such a trait as one can implement IsSpuriousError for WorksError itself, downcasting to the concrete Error types of the crates to call the is_spurious() implementations.

Could there be a better way?

Thanks for your advice 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions