Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Error Handling #438

Merged
merged 9 commits into from
Aug 27, 2020
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: std::error::Error + Send + 'static,
{
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 examples/handlers/stateful/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use gotham::error::Result;
use gotham::anyhow::Result;
use gotham::handler::{Handler, HandlerFuture, IntoResponse, NewHandler};
use gotham::router::builder::*;
use gotham::router::Router;
Expand Down
6 changes: 2 additions & 4 deletions gotham/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ serde = "1.0"
serde_derive = "1.0"
bincode = "1.0"
mime = "0.3.15"
# Using alpha version of mime_guess until mime crate stabilizes (releases 1.0).
# see https://github.com/hyperium/mime/issues/52
mime_guess = "2.0.1"
futures = "0.3.1"
tokio = { version = "0.2.6", features = ["full"] }
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.8", features = ["v4"] }
chrono = "0.4"
base64 = "0.12"
Expand All @@ -47,8 +45,8 @@ regex = "1.0"
cookie = "0.14"
http = "0.2"
httpdate = "0.3"
failure = "0.1"
itertools = "0.9.0"
anyhow = "1.0"
tokio-rustls = { version = "0.14.0", optional = true }

[dev-dependencies]
Expand Down
11 changes: 0 additions & 11 deletions gotham/src/error.rs

This file was deleted.

10 changes: 5 additions & 5 deletions gotham/src/handler/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

mod accepted_encoding;

use crate::error::Result;
use bytes::{BufMut, Bytes, BytesMut};
use futures::prelude::*;
use futures::ready;
Expand All @@ -23,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 @@ -165,15 +164,15 @@ impl DirHandler {
impl NewHandler for FileHandler {
type Instance = Self;

fn new_handler(&self) -> Result<Self::Instance> {
fn new_handler(&self) -> anyhow::Result<Self::Instance> {
Ok(self.clone())
}
}

impl NewHandler for DirHandler {
type Instance = Self;

fn new_handler(&self) -> Result<Self::Instance> {
fn new_handler(&self) -> anyhow::Result<Self::Instance> {
Ok(self.clone())
}
}
Expand Down Expand Up @@ -247,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