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

Added support for thiserror #18

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions tests/with_thiserror_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::anyhow;
use displaydoc::Display;
use std::error::Error as _;
use std::error::Error as StdError;
use std::io;
use thiserror::Error;

Expand All @@ -20,13 +20,13 @@ fn test_transparent_for_enum() {

let var = MyError::Variant(anyhow!("inner").context("outer"));
assert_display(&var, "outer");
// assert_eq!(var.source().unwrap().to_string(), "inner")
assert_eq!(var.source().unwrap().to_string(), "inner")
}

#[test]
fn test_transparent_for_struct() {
#[derive(Display, Error, Debug)]
#[display(transparent)]
#[error(transparent)]
struct Error(ErrorKind);

#[derive(Display, Error, Debug)]
Expand All @@ -44,7 +44,7 @@ fn test_transparent_for_struct() {
let io = io::Error::new(io::ErrorKind::Other, "oh no!");
let error = Error(ErrorKind::from(io));
assert_eq!("E1", error.to_string());
// error.source().unwrap().downcast_ref::<io::Error>().unwrap();
error.source().unwrap().downcast_ref::<io::Error>().unwrap();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only test failing now.

}

#[test]
Expand All @@ -68,3 +68,31 @@ fn test_errordoc_for_struct() {
}
assert_display(MyError { variant: 42 }, "I'm a doc for MyError");
}

#[test]
fn test_thiserror_implicit_and_source_works() {
#[derive(Display, Error, Debug)]
#[error("implicit source")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are only passing because you're using error to derive the display which causes thiserror to do it.

Try doing an enum like this as a test case

#[derive(Debug, Display, Error)]
enum SourceError {
    #[error(transparent)]
    #[display(transparent)]
    ImplicitSource {
        source: io::Error,
    },
    #[error(transparent)]
    #[display(transparent)]
    ExplicitSource {
        source: String,
        #[source]
        io: io::Error,
    },
    /// There isnt really a {source}
    DocSourceless {
         source: String,
    },
}

struct ImplicitSource {
source: io::Error,
}

#[derive(Display, Error, Debug)]
#[error("explicit source")]
struct ExplicitSource {
source: String,
#[source]
io: io::Error,
}

let io = io::Error::new(io::ErrorKind::Other, "oh no!");
let error = ImplicitSource { source: io };
error.source().unwrap().downcast_ref::<io::Error>().unwrap();

let io = io::Error::new(io::ErrorKind::Other, "oh no!");
let error = ExplicitSource {
source: String::new(),
io,
};
error.source().unwrap().downcast_ref::<io::Error>().unwrap();
}