Skip to content

Limitations of downcast_ref #456

@martinstuder

Description

@martinstuder

I encounter situations where unwrap_err().downcast_ref() on a miette::Result always returns None despite the correct error type being contained. The situation typically happens in tests where the error type that I'm trying to downcast to is defined in the same workspace member / package. If the error type is defined in another workspace member, then downcast_ref seems to work just fine. I'm wondering why this is the case and whether there are specific limitations that need to be considered.

My error types are typically defined as follows:

use miette::Diagnostic;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Error, Diagnostic)]
pub enum MyError {
    #[error("some error")]
    #[diagnostic(code(myerror::some_error))]
    SomeError,
}

And tests that I'm referring to have the following structure:

#[cfg(test)]
mod tests {
    #[test]
    fn test_some_error() {
        assert_eq!(do_something().unwrap_err().downcast_ref(), Some(&MyError::SomeError));
    }
}

where do_something() returns a miette::Result.

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