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

Replace () error types with specific types that implement Error #299

Open
brson opened this issue May 4, 2017 · 12 comments
Open

Replace () error types with specific types that implement Error #299

brson opened this issue May 4, 2017 · 12 comments
Assignees

Comments

@brson
Copy link
Contributor

brson commented May 4, 2017

There are a number of methods in url that return Result<_, ()>. () though does not implement Error and so does not inter operate cleanly with callers that want to treat the result as Error (like error-chain).

In today's Rust the best pattern for this is probably to create a single Error enum for the entire crate to share and return it everywhere.

Would require a major version bump.

@dtolnay
Copy link
Contributor

dtolnay commented May 12, 2017

Yep we struggled with this in #323. It makes it hard to handle these errors concisely.

@Enet4
Copy link
Contributor

Enet4 commented May 12, 2017

Here's a list of public methods returning () as an error:

  1. Url#path_segments_mut
  2. Url#set_port
  3. Url#set_ip_host
  4. Url#set_password
  5. Url#set_username
  6. Url#set_scheme
  7. Url#from_file_path
  8. Url#from_directory_path
  9. Url#to_file_path

Although Url#path_segments returns an Option, this is not very consistent when we look at its "_mut" counterpart path_segments_mut, which returns a Result.

I would have suggested a CannotBeBase variant of ParseError for all methods requiring a URL that is not cannot-be-base (this includes methods 1 to 6 above), but it seems that the current design is to have one for each method. Should we change that to make the error enum more concise? I cannot think of a good reason to distinguish SetHostCannotBeBase from PathSegmentsCannotBeBase or SetSchemeCannotBeBase.

Methods 7 and 8 could return a new variant ParseError::PathNotAbsolute. Method 9 could yield a new ParseError::InvalidLocalPath.

Let's not also forget Url#with_default_port, which takes an argument of type FnOnce() -> Result<u16, ()>. Once we harmonize all the results, we might be able to just replace the error type with ParseError.

bors-servo pushed a commit that referenced this issue Jun 13, 2017
Replace unwrap() in doctests with `?`

Fixes #312.
 - All results yielding `ParseError` in the doctests should now be handled with the ? operator.
 - Some other specific results are ~unwrapped with `expect()`~ mapped to a placeholder string message, such as in URLs that are not _cannot-be-a-base_. Users are then expected to adjust this mapping depending on their use case. Once #299 is resolved, we'll be able to propagate these normally.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/323)
<!-- Reviewable:end -->
@tmccombs
Copy link
Contributor

I'll start working on this.

tmccombs added a commit to tmccombs/rust-url that referenced this issue Jul 15, 2019
This is an initial pass at servo#299.

It does not change `ParseError` to the more idiomatic `Error` name, or
change `with_default_port`'s return type.
@nox
Copy link
Contributor

nox commented Jul 17, 2019

I would have suggested a CannotBeBase variant of ParseError for all methods requiring a URL that is not cannot-be-base (this includes methods 1 to 6 above)

1 to 6 don't exactly have the same error conditions, actually.

  • 2 wants an non-file: URL with a host.
  • 4-6 want a URL with a host.

@nox
Copy link
Contributor

nox commented Jul 17, 2019

I guess for 4-6 we could return EmptyHost, but I am a bit weirded out by the self.host() == Some(Host::Domain("")) test in set_password which is not found in the others.

@tmccombs
Copy link
Contributor

In my PR I went with EmptyHost for 4-6. But would also be ok with making a new error type for them.

tmccombs added a commit to tmccombs/rust-url that referenced this issue Jul 18, 2019
This is an initial pass at servo#299.

It does not change `ParseError` to the more idiomatic `Error` name, or
change `with_default_port`'s return type.
@nox nox self-assigned this Jul 18, 2019
@bstrie
Copy link

bstrie commented Oct 31, 2019

@tmccombs Are you pursuing this? If not, I might be interested in resurrecting your patch.

@tmccombs
Copy link
Contributor

tbh, I'm not entirely sure what to do with this, since I missed the last major release and this is a backwards incompatible change.

@bstrie
Copy link

bstrie commented Nov 1, 2019

@tmccombs I have an idea for a way to make this happen that wouldn't involve a breaking change, I'll take a crack at it based on your PR.

@djc
Copy link
Contributor

djc commented Aug 19, 2020

As I understand it, this change would be semver-incompatible. I've opened a tracking issue in #627 for proposed semver-incompatible changes to discuss how these could be handled going forward.

@hoijui
Copy link

hoijui commented Oct 21, 2021

This is also an issue when using the thiserror create, as one can not easily wrapp () with thiserrors #[from].

@mhnap
Copy link

mhnap commented Oct 13, 2023

Maybe there are some updates on this issue? Or some help is needed to fix it?

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

9 participants