Skip to content

Commit

Permalink
refactor HandlerError to implement From<E> for all E: Into<anyhow::Er…
Browse files Browse the repository at this point in the history
…ror>
  • Loading branch information
msrd0 committed Jul 6, 2020
1 parent f2fe29d commit 08292b5
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 135 deletions.
19 changes: 6 additions & 13 deletions examples/diesel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate diesel_migrations;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use futures::prelude::*;
use gotham::handler::{HandlerError, HandlerFuture, IntoHandlerError};
use gotham::handler::{HandlerError, HandlerFuture, MapHandlerError, MapHandlerErrorFuture};
use gotham::helpers::http::response::create_response;
use gotham::hyper::{body, Body, StatusCode};
use gotham::pipeline::{new_pipeline, single::single_pipeline};
Expand Down Expand Up @@ -62,7 +62,7 @@ fn create_product_handler(mut state: State) -> Pin<Box<HandlerFuture>> {

let rows = match query_result {
Ok(rows) => rows,
Err(e) => return Err((state, e.into_handler_error())),
Err(e) => return Err((state, e.into())),
};

let body =
Expand All @@ -85,7 +85,7 @@ fn get_products_handler(state: State) -> Pin<Box<HandlerFuture>> {
let res = create_response(&state, StatusCode::OK, mime::APPLICATION_JSON, body);
Ok((state, res))
}
Err(e) => Err((state, e.into_handler_error())),
Err(e) => Err((state, e.into())),
}
}
.boxed()
Expand All @@ -103,24 +103,17 @@ fn router(repo: Repo) -> Router {
})
}

fn bad_request<E>(e: E) -> HandlerError
where
E: IntoHandlerError,
{
e.into_handler_error().with_status(StatusCode::BAD_REQUEST)
}

async fn extract_json<T>(state: &mut State) -> Result<T, HandlerError>
where
T: serde::de::DeserializeOwned,
{
let body = body::to_bytes(Body::take_from(state))
.map_err(bad_request)
.map_err_with_status(StatusCode::BAD_REQUEST)
.await?;
let b = body.to_vec();
from_utf8(&b)
.map_err(bad_request)
.and_then(|s| serde_json::from_str::<T>(s).map_err(bad_request))
.map_err_with_status(StatusCode::BAD_REQUEST)
.and_then(|s| serde_json::from_str::<T>(s).map_err_with_status(StatusCode::BAD_REQUEST))
}

/// Start a server and use a `Router` to dispatch requests
Expand Down
8 changes: 4 additions & 4 deletions examples/handlers/async_handlers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use gotham::hyper::StatusCode;
#[cfg(not(test))]
use gotham::hyper::{body, Client, Uri};

use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::handler::{HandlerFuture};
use gotham::helpers::http::response::create_response;
use gotham::router::builder::DefineSingleRoute;
use gotham::router::builder::{build_simple_router, DrawRoutes};
Expand Down Expand Up @@ -108,7 +108,7 @@ fn series_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
println!("series length: {} finished", length);
future::ok((state, res))
}
Err(err) => future::err((state, err.into_handler_error())),
Err(err) => future::err((state, err.into())),
})
.boxed()
}
Expand Down Expand Up @@ -162,7 +162,7 @@ fn loop_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
println!("loop length: {} finished", length);
future::ok((state, res))
}
Err(err) => future::err((state, err.into_handler_error())),
Err(err) => future::err((state, err.into())),
})
.boxed()
}
Expand Down Expand Up @@ -230,7 +230,7 @@ fn parallel_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
println!("parallel length: {} finished", length);
future::ok((state, res))
}
Err(err) => future::err((state, err.into_handler_error())),
Err(err) => future::err((state, err.into())),
})
.boxed()
}
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers/form_urlencoded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gotham::hyper::{body, Body, StatusCode};
use std::pin::Pin;
use url::form_urlencoded;

use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::handler::{HandlerFuture};
use gotham::helpers::http::response::create_response;
use gotham::router::builder::{build_simple_router, DefineSingleRoute, DrawRoutes};
use gotham::router::Router;
Expand All @@ -25,7 +25,7 @@ fn form_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, res_body);
future::ok((state, res))
}
Err(e) => future::err((state, e.into_handler_error())),
Err(e) => future::err((state, e.into())),
});

f.boxed()
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers/multipart/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! An example of decoding multipart form requests
use futures::prelude::*;
use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::handler::{HandlerFuture};
use gotham::helpers::http::response::create_response;
use gotham::hyper::header::CONTENT_TYPE;
use gotham::hyper::{body, Body, HeaderMap, StatusCode};
Expand Down Expand Up @@ -63,7 +63,7 @@ fn form_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
}
}
}
Err(e) => future::err((state, e.into_handler_error())),
Err(e) => future::err((state, e.into())),
})
.boxed()
}
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers/request_data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures::prelude::*;
use gotham::hyper::{body, Body, HeaderMap, Method, Response, StatusCode, Uri, Version};
use std::pin::Pin;

use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::handler::{HandlerFuture};
use gotham::helpers::http::response::create_empty_response;
use gotham::router::builder::{build_simple_router, DefineSingleRoute, DrawRoutes};
use gotham::router::Router;
Expand Down Expand Up @@ -31,7 +31,7 @@ fn post_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
let res = create_empty_response(&state, StatusCode::OK);
future::ok((state, res))
}
Err(e) => future::err((state, e.into_handler_error())),
Err(e) => future::err((state, e.into())),
});

f.boxed()
Expand Down
2 changes: 1 addition & 1 deletion gotham/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bytes = "0.5"
mio = "0.7"
borrow-bag = { path = "../misc/borrow_bag", version = "1.0" }
percent-encoding = "2.1"
pin-project = "0.4.2"
pin-project = "0.4.20"
uuid = { version = "0.7", features = ["v4"] }
chrono = "0.4"
base64 = "0.12"
Expand Down
5 changes: 3 additions & 2 deletions gotham/src/handler/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use tokio::fs::File;
use tokio::io::AsyncRead;

use self::accepted_encoding::accepted_encodings;
use crate::handler::{Handler, HandlerFuture, IntoHandlerError, NewHandler};
use crate::handler::{Handler, HandlerError, HandlerFuture, NewHandler};
use crate::router::response::extender::StaticResponseExtender;
use crate::state::{FromState, State, StateData};

Expand Down Expand Up @@ -246,7 +246,8 @@ fn create_file_response(options: FileOptions, state: State) -> Pin<Box<HandlerFu
io::ErrorKind::PermissionDenied => StatusCode::FORBIDDEN,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
Err((state, err.into_handler_error().with_status(status)))
let err: HandlerError = err.into();
Err((state, err.with_status(status)))
}
})
.boxed()
Expand Down
Loading

0 comments on commit 08292b5

Please sign in to comment.