Examples full of unsafe usage of .unwrap()
I've just looked through the examples and documentation how to write a good handlers. And all over the place, the it uses .unwrap on Results which could also be Err. This leads to panics in rust - basically what Cloudflare took down last week.
Here are a couple of unsafe unwraps:
- https://github.com/hyperium/hyper/blob/be18a92bb51498a2113dcfe15d9da09e1c6c7b0d/examples/service_struct_impl.rs#L54
- https://github.com/hyperium/hyper/blob/be18a92bb51498a2113dcfe15d9da09e1c6c7b0d/examples/send_file.rs#L64
- https://github.com/hyperium/hyper/blob/be18a92bb51498a2113dcfe15d9da09e1c6c7b0d/examples/web_api.rs#L32
- https://github.com/hyperium/hyper/blob/be18a92bb51498a2113dcfe15d9da09e1c6c7b0d/examples/upgrades.rs#L103
And when I would now remove these unwraps+ the Ok and directly try to return the Result... it would now work because these Response builders are using as Error type hyper::http::Errors and all the examples are build around hyper::Error.
The examples are basically only showing us how to not to write good servers.
Hey, thanks for taking a look!
I looked at each case of unwrap in those examples, and they are indeed all safe. However, it may non-obvious, so .expect could be the better option to document why. For instance, the http builders can return an error if one of the values passed to uri(), header(), or status() are not valid values for that type. The examples are all calling them with constants that are known to be good, so the unwraps are safe.
But again, for the sake of teaching, it could be a good idea to change those to .expect() explaining why they are fine. Or... (and this is what I do typically in my own code), they could just not used the builders, and thus show that the constants are safe with things like from_static().