diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b9fe8b..369a09b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support custom HTTP response headers - New `endpoint` router extension with associated `Endpoint` trait ([!18]) - Support for custom endpoints using the `#[endpoint]` macro ([!19]) + - Support for `anyhow::Error` (or any type implementing `Into`) in most responses ### Changed - The cors handler can now copy headers from the request if desired diff --git a/src/openapi/handler.rs b/src/openapi/handler.rs index fc32c11..f340a6d 100644 --- a/src/openapi/handler.rs +++ b/src/openapi/handler.rs @@ -1,5 +1,5 @@ +#![cfg_attr(not(feature = "auth"), allow(unused_imports))] use super::SECURITY_NAME; - use futures_util::{future, future::FutureExt}; use gotham::{ anyhow, @@ -67,7 +67,7 @@ fn get_security(state: &mut State) -> IndexMap (Vec, IndexMap>) { +fn get_security(_state: &mut State) -> IndexMap> { Default::default() } diff --git a/src/result/auth_result.rs b/src/result/auth_result.rs index 90e564e..1b1fd7f 100644 --- a/src/result/auth_result.rs +++ b/src/result/auth_result.rs @@ -70,9 +70,9 @@ impl From for AuthErrorOrOther { } mod private { - use gotham::anyhow; + use gotham::handler::HandlerError; pub trait Sealed {} - impl> Sealed for E {} + impl> Sealed for E {} } impl From for AuthErrorOrOther diff --git a/src/result/mod.rs b/src/result/mod.rs index deb9d12..6d699bf 100644 --- a/src/result/mod.rs +++ b/src/result/mod.rs @@ -3,12 +3,12 @@ use crate::OpenapiSchema; use crate::Response; use futures_util::future::FutureExt; +use gotham::handler::HandlerError; #[cfg(feature = "openapi")] use gotham::hyper::StatusCode; use mime::{Mime, STAR_STAR}; use serde::Serialize; use std::{ - error::Error, fmt::{Debug, Display}, future::Future, pin::Pin @@ -45,7 +45,7 @@ impl OrAllTypes for Option> { /// A trait provided to convert a resource's result to json. pub trait ResourceResult { - type Err: Error + Send + Sync + 'static; + type Err: Into + Send + Sync + 'static; /// Turn this into a response that can be returned to the browser. This api will likely /// change in the future. diff --git a/src/result/redirect.rs b/src/result/redirect.rs index 5754adc..432203d 100644 --- a/src/result/redirect.rs +++ b/src/result/redirect.rs @@ -1,7 +1,7 @@ -use super::{handle_error, NoContent, ResourceResult}; -#[cfg(feature = "openapi")] -use crate::OpenapiSchema; +use super::{handle_error, ResourceResult}; use crate::{IntoResponseError, Response}; +#[cfg(feature = "openapi")] +use crate::{NoContent, OpenapiSchema}; use futures_util::future::{BoxFuture, FutureExt, TryFutureExt}; use gotham::hyper::{ header::{InvalidHeaderValue, LOCATION}, @@ -78,7 +78,7 @@ pub enum RedirectError { impl ResourceResult for Result where E: Display + IntoResponseError, - ::Err: Sync + ::Err: StdError + Sync { type Err = RedirectError<::Err>; diff --git a/src/result/result.rs b/src/result/result.rs index 2b8afe6..71c44b7 100644 --- a/src/result/result.rs +++ b/src/result/result.rs @@ -9,7 +9,7 @@ use mime::{Mime, APPLICATION_JSON}; use std::{error::Error, fmt::Display, pin::Pin}; pub trait IntoResponseError { - type Err: Error + Send + 'static; + type Err: Display + Send + 'static; fn into_response_error(self) -> Result; } diff --git a/src/routing.rs b/src/routing.rs index f20bbf7..132eed6 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -8,6 +8,8 @@ use crate::{ Endpoint, FromBody, Resource, Response }; +#[cfg(feature = "cors")] +use gotham::router::route::matcher::AccessControlRequestMethodMatcher; use gotham::{ handler::HandlerError, helpers::http::response::{create_empty_response, create_response}, @@ -16,9 +18,7 @@ use gotham::{ router::{ builder::{DefineSingleRoute, DrawRoutes, RouterBuilder, ScopeBuilder}, non_match::RouteNonMatch, - route::matcher::{ - AcceptHeaderRouteMatcher, AccessControlRequestMethodMatcher, ContentTypeHeaderRouteMatcher, RouteMatcher - } + route::matcher::{AcceptHeaderRouteMatcher, ContentTypeHeaderRouteMatcher, RouteMatcher} }, state::{FromState, State} }; @@ -87,7 +87,11 @@ fn response_from(res: Response, state: &State) -> gotham::hyper::Response r } -async fn endpoint_handler(state: &mut State) -> Result, HandlerError> { +async fn endpoint_handler(state: &mut State) -> Result, HandlerError> +where + E: Endpoint, + ::Err: Into +{ trace!("entering endpoint_handler"); let placeholders = E::Placeholders::take_from(state); let params = E::Params::take_from(state); @@ -120,7 +124,7 @@ async fn endpoint_handler(state: &mut State) -> Result