mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-22 20:52:27 +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
|
||||
- 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<HandlerError>`) in most responses
|
||||
|
||||
### Changed
|
||||
- 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 futures_util::{future, future::FutureExt};
|
||||
use gotham::{
|
||||
anyhow,
|
||||
|
@ -67,7 +67,7 @@ fn get_security(state: &mut State) -> IndexMap<String, ReferenceOr<SecuritySchem
|
|||
}
|
||||
|
||||
#[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()
|
||||
}
|
||||
|
||||
|
|
|
@ -70,9 +70,9 @@ impl<E> From<AuthError> for AuthErrorOrOther<E> {
|
|||
}
|
||||
|
||||
mod private {
|
||||
use gotham::anyhow;
|
||||
use gotham::handler::HandlerError;
|
||||
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>
|
||||
|
|
|
@ -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<Vec<Mime>> {
|
|||
|
||||
/// A trait provided to convert a resource's result to json.
|
||||
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
|
||||
/// change in the future.
|
||||
|
|
|
@ -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<E: StdError + 'static> {
|
|||
impl<E> ResourceResult for Result<Redirect, E>
|
||||
where
|
||||
E: Display + IntoResponseError,
|
||||
<E as IntoResponseError>::Err: Sync
|
||||
<E as IntoResponseError>::Err: StdError + Sync
|
||||
{
|
||||
type Err = RedirectError<<E as IntoResponseError>::Err>;
|
||||
|
||||
|
|
|
@ -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<Response, Self::Err>;
|
||||
}
|
||||
|
|
|
@ -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<Body>
|
|||
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");
|
||||
let placeholders = E::Placeholders::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 res = out.into_response().await?;
|
||||
let res = out.into_response().await.map_err(Into::into)?;
|
||||
debug!("Returning response {:?}", res);
|
||||
Ok(response_from(res, state))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue