hyperium / hyper

An HTTP library for Rust
https://hyper.rs
MIT License
14.42k stars 1.59k forks source link

URI parse errors should preserve information from the HTTP crate #3043

Open hawkw opened 1 year ago

hawkw commented 1 year ago

Is your feature request related to a problem? Please describe.

When an invalid URI is parsed by the http crate, it returns an InvalidUri error. When hyper encounters one of these errors, it converts it into a hyper::Error type with an error kind that indicates that an invalid URI was encountered. The http::uri::InvalidUri type contains additional information describing what part of the URI was invalid, which is included in the error's fmt::Display output: https://github.com/hyperium/http/blob/c1f309e0c3695f8e14e1b54c738681783d4c7b9e/src/uri/mod.rs#L1063-L1079

Unfrortunately, when hyper converts these errors into its hyper::Error type, the additional information is discarded: https://github.com/hyperium/hyper/blob/75aac9f47fe0246016e6133cd3cfa35b63c8904e/src/error.rs#L489-L499

The error that's returned to the application will format as just "invalid URI", which is not particularly descriptive. Being able to surface more information about why a URI was invalid would be useful, particularly in applications where URIs are input by the user.

Describe the solution you'd like

It would be nice if hyper preserved the description of what part of the URI was invalid. I think we should probably change the Parse::Uri variant in hyper::error to contain an http::uri::InvalidUri, so that we can format the additional error information in the fmt::Display impl for hyper::Error.

Since we don't want to expose types from other crates in hyper's public API, for stability reasons, we probably shouldn't allow the user to access the http::uri::InvalidUri error in the hyper::Error source chain, or allow downcasting the error to it.

Describe alternatives you've considered

hawkw commented 1 year ago

I'm happy to put together a quick PR for this, but I figured it was worth opening an issue first.

LucioFranco commented 1 year ago

Why not let users downcast it? If http will also 1.0 I don't see the issue in that? Otherwise, I agree with what you said.

hawkw commented 1 year ago

Why not let users downcast it? If http will also 1.0 I don't see the issue in that? Otherwise, I agree with what you said.

When http 1.0 is released, we should probably make it available as a source/downcast target. In the meantime, it would still be better to at least display it in fmt::Display.