mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
replace some std::error::Error
bounds with Into<HandlerError>
This commit is contained in:
parent
9e65540cd8
commit
8b73701405
7 changed files with 21 additions and 16 deletions
|
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Support custom HTTP response headers
|
- Support custom HTTP response headers
|
||||||
- New `endpoint` router extension with associated `Endpoint` trait ([!18])
|
- New `endpoint` router extension with associated `Endpoint` trait ([!18])
|
||||||
- Support for custom endpoints using the `#[endpoint]` macro ([!19])
|
- Support for custom endpoints using the `#[endpoint]` macro ([!19])
|
||||||
|
- Support for `anyhow::Error` (or any type implementing `Into<HandlerError>`) in most responses
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- The cors handler can now copy headers from the request if desired
|
- The cors handler can now copy headers from the request if desired
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
#![cfg_attr(not(feature = "auth"), allow(unused_imports))]
|
||||||
use super::SECURITY_NAME;
|
use super::SECURITY_NAME;
|
||||||
|
|
||||||
use futures_util::{future, future::FutureExt};
|
use futures_util::{future, future::FutureExt};
|
||||||
use gotham::{
|
use gotham::{
|
||||||
anyhow,
|
anyhow,
|
||||||
|
@ -67,7 +67,7 @@ fn get_security(state: &mut State) -> IndexMap<String, ReferenceOr<SecuritySchem
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "auth"))]
|
#[cfg(not(feature = "auth"))]
|
||||||
fn get_security(state: &mut State) -> (Vec<SecurityRequirement>, IndexMap<String, ReferenceOr<SecurityScheme>>) {
|
fn get_security(_state: &mut State) -> IndexMap<String, ReferenceOr<SecurityScheme>> {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,9 +70,9 @@ impl<E> From<AuthError> for AuthErrorOrOther<E> {
|
||||||
}
|
}
|
||||||
|
|
||||||
mod private {
|
mod private {
|
||||||
use gotham::anyhow;
|
use gotham::handler::HandlerError;
|
||||||
pub trait Sealed {}
|
pub trait Sealed {}
|
||||||
impl<E: Into<anyhow::Error>> Sealed for E {}
|
impl<E: Into<HandlerError>> Sealed for E {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E, F> From<F> for AuthErrorOrOther<E>
|
impl<E, F> From<F> for AuthErrorOrOther<E>
|
||||||
|
|
|
@ -3,12 +3,12 @@ use crate::OpenapiSchema;
|
||||||
use crate::Response;
|
use crate::Response;
|
||||||
|
|
||||||
use futures_util::future::FutureExt;
|
use futures_util::future::FutureExt;
|
||||||
|
use gotham::handler::HandlerError;
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
use gotham::hyper::StatusCode;
|
use gotham::hyper::StatusCode;
|
||||||
use mime::{Mime, STAR_STAR};
|
use mime::{Mime, STAR_STAR};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin
|
pin::Pin
|
||||||
|
@ -45,7 +45,7 @@ impl OrAllTypes for Option<Vec<Mime>> {
|
||||||
|
|
||||||
/// A trait provided to convert a resource's result to json.
|
/// A trait provided to convert a resource's result to json.
|
||||||
pub trait ResourceResult {
|
pub trait ResourceResult {
|
||||||
type Err: Error + Send + Sync + 'static;
|
type Err: Into<HandlerError> + Send + Sync + 'static;
|
||||||
|
|
||||||
/// Turn this into a response that can be returned to the browser. This api will likely
|
/// Turn this into a response that can be returned to the browser. This api will likely
|
||||||
/// change in the future.
|
/// change in the future.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::{handle_error, NoContent, ResourceResult};
|
use super::{handle_error, ResourceResult};
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
use crate::OpenapiSchema;
|
|
||||||
use crate::{IntoResponseError, Response};
|
use crate::{IntoResponseError, Response};
|
||||||
|
#[cfg(feature = "openapi")]
|
||||||
|
use crate::{NoContent, OpenapiSchema};
|
||||||
use futures_util::future::{BoxFuture, FutureExt, TryFutureExt};
|
use futures_util::future::{BoxFuture, FutureExt, TryFutureExt};
|
||||||
use gotham::hyper::{
|
use gotham::hyper::{
|
||||||
header::{InvalidHeaderValue, LOCATION},
|
header::{InvalidHeaderValue, LOCATION},
|
||||||
|
@ -78,7 +78,7 @@ pub enum RedirectError<E: StdError + 'static> {
|
||||||
impl<E> ResourceResult for Result<Redirect, E>
|
impl<E> ResourceResult for Result<Redirect, E>
|
||||||
where
|
where
|
||||||
E: Display + IntoResponseError,
|
E: Display + IntoResponseError,
|
||||||
<E as IntoResponseError>::Err: Sync
|
<E as IntoResponseError>::Err: StdError + Sync
|
||||||
{
|
{
|
||||||
type Err = RedirectError<<E as IntoResponseError>::Err>;
|
type Err = RedirectError<<E as IntoResponseError>::Err>;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ use mime::{Mime, APPLICATION_JSON};
|
||||||
use std::{error::Error, fmt::Display, pin::Pin};
|
use std::{error::Error, fmt::Display, pin::Pin};
|
||||||
|
|
||||||
pub trait IntoResponseError {
|
pub trait IntoResponseError {
|
||||||
type Err: Error + Send + 'static;
|
type Err: Display + Send + 'static;
|
||||||
|
|
||||||
fn into_response_error(self) -> Result<Response, Self::Err>;
|
fn into_response_error(self) -> Result<Response, Self::Err>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ use crate::{
|
||||||
Endpoint, FromBody, Resource, Response
|
Endpoint, FromBody, Resource, Response
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "cors")]
|
||||||
|
use gotham::router::route::matcher::AccessControlRequestMethodMatcher;
|
||||||
use gotham::{
|
use gotham::{
|
||||||
handler::HandlerError,
|
handler::HandlerError,
|
||||||
helpers::http::response::{create_empty_response, create_response},
|
helpers::http::response::{create_empty_response, create_response},
|
||||||
|
@ -16,9 +18,7 @@ use gotham::{
|
||||||
router::{
|
router::{
|
||||||
builder::{DefineSingleRoute, DrawRoutes, RouterBuilder, ScopeBuilder},
|
builder::{DefineSingleRoute, DrawRoutes, RouterBuilder, ScopeBuilder},
|
||||||
non_match::RouteNonMatch,
|
non_match::RouteNonMatch,
|
||||||
route::matcher::{
|
route::matcher::{AcceptHeaderRouteMatcher, ContentTypeHeaderRouteMatcher, RouteMatcher}
|
||||||
AcceptHeaderRouteMatcher, AccessControlRequestMethodMatcher, ContentTypeHeaderRouteMatcher, RouteMatcher
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
state::{FromState, State}
|
state::{FromState, State}
|
||||||
};
|
};
|
||||||
|
@ -87,7 +87,11 @@ fn response_from(res: Response, state: &State) -> gotham::hyper::Response<Body>
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn endpoint_handler<E: Endpoint>(state: &mut State) -> Result<gotham::hyper::Response<Body>, HandlerError> {
|
async fn endpoint_handler<E: Endpoint>(state: &mut State) -> Result<gotham::hyper::Response<Body>, HandlerError>
|
||||||
|
where
|
||||||
|
E: Endpoint,
|
||||||
|
<E::Output as ResourceResult>::Err: Into<HandlerError>
|
||||||
|
{
|
||||||
trace!("entering endpoint_handler");
|
trace!("entering endpoint_handler");
|
||||||
let placeholders = E::Placeholders::take_from(state);
|
let placeholders = E::Placeholders::take_from(state);
|
||||||
let params = E::Params::take_from(state);
|
let params = E::Params::take_from(state);
|
||||||
|
@ -120,7 +124,7 @@ async fn endpoint_handler<E: Endpoint>(state: &mut State) -> Result<gotham::hype
|
||||||
};
|
};
|
||||||
|
|
||||||
let out = E::handle(state, placeholders, params, body).await;
|
let out = E::handle(state, placeholders, params, body).await;
|
||||||
let res = out.into_response().await?;
|
let res = out.into_response().await.map_err(Into::into)?;
|
||||||
debug!("Returning response {:?}", res);
|
debug!("Returning response {:?}", res);
|
||||||
Ok(response_from(res, state))
|
Ok(response_from(res, state))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue