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

try_from_* methods should either return an error-containing Result or an Option #73

Open
fralalonde opened this issue May 18, 2018 · 5 comments

Comments

@fralalonde
Copy link
Contributor

Considering the following DNSNameRef signatures

pub fn try_from_ascii(dns_name: untrusted::Input<'a>) -> Result<Self, ()> {
pub fn try_from_ascii_str(dns_name: &str) -> Result<DNSNameRef, ()> {

From an API standpoint, I think returning a Result that has () for an error type is not good design.

If multiple libraries adopt this pattern, it is not possible to differentiate between the () from lib A and () from lib B. Which forces any error to be map_err() / handled immediately by the calling code when most often a simple ? would be preferable. It would otherwise be preposterous to force applications to assume that any () error result originates only from webpki, or to require that any error of type () be considered of unknown origin.

If a Result<> has no error context to provide, an error-less Option<> should be returned instead.

@briansmith
Copy link
Owner

It seems like these functions should have an error type like struct NotADNSName; Then people can write their From<NotADNSName> conversions to the more specific errors.

Error is used instead of Option because we're trying to follow the gist of TryFrom from rust-lang/rust#33417. My plan is, once TryFrom is available, to switch to using TryFrom for these versions.

@briansmith
Copy link
Owner

Note that in this crate and in ring we avoid most error infrastructure. In particular, we avoid implementing std::error::Error (it's poorly designed) and we avoid using the failure and error_chain crates intentionally (their too heavyweight and I generally avoid macros when practical).

@fralalonde
Copy link
Contributor Author

These choices and explanations are perfectly reasonable. Too bad TryFrom has been pushed back.

Introducing NotADNSName as suggested would provide a good interim solution.

@briansmith
Copy link
Owner

Introducing NotADNSName as suggested would provide a good interim solution.

I would take a PR that does this.

@fralalonde
Copy link
Contributor Author

I followed the idea you expressed, let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants