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

all Error types should be public? #39381

Closed
getreu opened this issue Jan 29, 2017 · 6 comments
Closed

all Error types should be public? #39381

getreu opened this issue Jan 29, 2017 · 6 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@getreu
Copy link

getreu commented Jan 29, 2017

In order to test the error behaviour of a function you need an error struct to compare with. For example:
since ParseIntError is private, I have to generate one by doing something wrong, like u8::from_str("257"). This leads to the following test:

let pie_overflow      = u8::from_str("257").unwrap_err();
assert_eq!(super::Missions::parse_enc_opt("ascii,256,U+41..U+67",8).unwrap_err(), pie_overflow );

If error types where public I would rather instantiate one and write:

let pie_overflow  = ParseIntError {kind: std::num::Overflow};
assert_eq!(super::Missions::parse_enc_opt("ascii,256,U+41..U+67",8).unwrap_err(), pie_overflow );

Maybe there is a reason I do not see why they are private and there is better more idiomatic way of doing error comparison?

@getreu getreu changed the title all Error types should be public all Error types should be public? Jan 29, 2017
@niconii
Copy link
Contributor

niconii commented Jan 29, 2017

Maybe a dupe of #22639, though this might be more about a general principle for Error types.

@BurntSushi BurntSushi added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jan 29, 2017
@Mark-Simulacrum
Copy link
Member

@rust-lang/libs, this seems relevant to the libz blitz and associated API guideline generation.

@dtolnay
Copy link
Member

dtolnay commented May 20, 2017

Thanks! I filed rust-lang/api-guidelines#69 to follow up with a guideline.

@aturon
Copy link
Member

aturon commented May 23, 2017

We discussed this in the libs team meeting today. The problem with allowing public constructors is that it precludes you ever extending the error with additional fields or variants.

An alternative is to make sure there are enough public accessors that you can write tests using the accessors, rather than by ginning up an error.

@aturon aturon closed this as completed May 23, 2017
@getreu
Copy link
Author

getreu commented May 24, 2017

An alternative is to make sure there are enough public accessors that you can write tests using the accessors, rather than by ginning up an error.
Thank you. Could you please point me to a piece of code how to implement it?

@aturon
Copy link
Member

aturon commented May 25, 2017

@getreu So for ParseIntError today, there's not any information in the error itself, so you just want to assert .is_err().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants