http-rs / http-types

Common types for HTTP operations
https://docs.rs/http-types
Apache License 2.0
200 stars 83 forks source link

Return `Result` from constructors which take `TryInto<Url>` #303

Open yoshuawuyts opened 3 years ago

yoshuawuyts commented 3 years ago

Now that TryFrom<str> for Url has been implemented, using the Url struct is a lot more pleasant. This opens up the possibility again for us to move from a "panic if parsing fails" to returning errors if parsing fails.

// current, panic if the url is malformed
let req = Request::post("https://api.foo.com/berries");

// proposed, throw an error instead
let req = Request::post("https://api.foo.com/berries")?;
brightly-salty commented 3 years ago

I'd be willing to attempt to implement this and submit a PR. Are you suggesting using something like the thiserror crate, or a custom error type with a custom std::error::Error impl?

yoshuawuyts commented 3 years ago

that's a good question; I think this should wrap the url::Url's error with the Status trait and send back a 500 status code. We don't need a custom impl or other crate since http-types already has its own Error type.