feat(error): revamp hyper::Error type
**The `Error` is now an opaque struct**, which allows for more variants to be added freely, and the internal representation to change without being breaking changes. For inspecting an `Error`, there are several `is_*` methods to check for certain classes of errors, such as `Error::is_parse()`. The `cause` can also be inspected, like before. This likely seems like a downgrade, but more inspection can be added as needed! The `Error` now knows about more states, which gives much more context around when a certain error occurs. This is also expressed in the description and `fmt` messages. **Most places where a user would provide an error to hyper can now pass any error type** (`E: Into<Box<std::error::Error>>`). This error is passed back in relevant places, and can be useful for logging. This should make it much clearer about what error a user should provide to hyper: any it feels is relevant! Closes #1128 Closes #1130 Closes #1431 Closes #1338 BREAKING CHANGE: `Error` is no longer an enum to pattern match over, or to construct. Code will need to be updated accordingly. For body streams or `Service`s, inference might be unable to determine what error type you mean to return. Starting in Rust 1.26, you could just label that as `!` if you never return an error.
This commit is contained in:
@@ -40,8 +40,9 @@ fn main() {
|
||||
println!("Response: {}", res.status());
|
||||
println!("Headers: {:#?}", res.headers());
|
||||
|
||||
res.into_parts().1.into_stream().for_each(|chunk| {
|
||||
io::stdout().write_all(&chunk).map_err(From::from)
|
||||
res.into_body().into_stream().for_each(|chunk| {
|
||||
io::stdout().write_all(&chunk)
|
||||
.map_err(|e| panic!("example expects stdout is open, error={}", e))
|
||||
})
|
||||
}).map(|_| {
|
||||
println!("\n\nDone.");
|
||||
|
||||
@@ -17,7 +17,8 @@ fn main() {
|
||||
let addr = ([127, 0, 0, 1], 3000).into();
|
||||
|
||||
let new_service = const_service(service_fn(|_| {
|
||||
Ok(Response::new(Body::from(PHRASE)))
|
||||
//TODO: when `!` is stable, replace error type
|
||||
Ok::<_, hyper::Error>(Response::new(Body::from(PHRASE)))
|
||||
}));
|
||||
|
||||
tokio::run(lazy(move || {
|
||||
|
||||
@@ -9,7 +9,6 @@ use futures::future::lazy;
|
||||
use futures::sync::oneshot;
|
||||
|
||||
use hyper::{Body, /*Chunk,*/ Method, Request, Response, StatusCode};
|
||||
use hyper::error::Error;
|
||||
use hyper::server::{Http, Service};
|
||||
|
||||
use std::fs::File;
|
||||
@@ -19,7 +18,7 @@ use std::thread;
|
||||
static NOTFOUND: &[u8] = b"Not Found";
|
||||
static INDEX: &str = "examples/send_file_index.html";
|
||||
|
||||
fn simple_file_send(f: &str) -> Box<Future<Item = Response<Body>, Error = hyper::Error> + Send> {
|
||||
fn simple_file_send(f: &str) -> Box<Future<Item = Response<Body>, Error = io::Error> + Send> {
|
||||
// Serve a file by reading it entirely into memory. As a result
|
||||
// this is limited to serving small files, but it is somewhat
|
||||
// simpler with a little less overhead.
|
||||
@@ -56,7 +55,7 @@ fn simple_file_send(f: &str) -> Box<Future<Item = Response<Body>, Error = hyper:
|
||||
};
|
||||
});
|
||||
|
||||
Box::new(rx.map_err(|e| Error::from(io::Error::new(io::ErrorKind::Other, e))))
|
||||
Box::new(rx.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
|
||||
}
|
||||
|
||||
struct ResponseExamples;
|
||||
@@ -64,7 +63,7 @@ struct ResponseExamples;
|
||||
impl Service for ResponseExamples {
|
||||
type Request = Request<Body>;
|
||||
type Response = Response<Body>;
|
||||
type Error = hyper::Error;
|
||||
type Error = io::Error;
|
||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error> + Send>;
|
||||
|
||||
fn call(&self, req: Request<Body>) -> Self::Future {
|
||||
@@ -119,7 +118,7 @@ impl Service for ResponseExamples {
|
||||
*/
|
||||
});
|
||||
|
||||
Box::new(rx.map_err(|e| Error::from(io::Error::new(io::ErrorKind::Other, e))))
|
||||
Box::new(rx.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
|
||||
},
|
||||
(&Method::GET, "/no_file.html") => {
|
||||
// Test what happens when file cannot be be found
|
||||
|
||||
Reference in New Issue
Block a user