-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The registered middlewares are now properly validation by `pavexc`: - they take `Next` as input (once) - they return a type that can be converted into a `Response` (either directly or in the happy case) - they don't use generic parameters that we can't reliably infer I did some refactorings along the way, especially in the component database (arguably the messiest part of the codebase).
- Loading branch information
1 parent
c74a941
commit 7b2481c
Showing
49 changed files
with
1,526 additions
and
496 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
error_handler: None, | ||
), | ||
], | ||
middlewares: [], | ||
routes: [ | ||
( | ||
path: "/home", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//! Execute common logic across multiple routes. | ||
//! | ||
//! Check out [`Blueprint::wrap`] for a brief introduction to wrapping middlewares in Pavex. | ||
//! | ||
//! [`Blueprint::wrap`]: crate::blueprint::Blueprint::wrap | ||
mod wrapping; | ||
|
||
pub use wrapping::WrappingMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::blueprint::{Blueprint, reflection::{RawCallable, RawCallableIdentifiers}, internals::RegisteredCallable}; | ||
|
||
/// The type returned by [`Blueprint::wrap`]. | ||
/// | ||
/// It allows you to further configure the behaviour of the registered wrapping | ||
/// middleware. | ||
pub struct WrappingMiddleware<'a> { | ||
#[allow(dead_code)] | ||
pub(crate) blueprint: &'a mut Blueprint, | ||
/// The index of the registered wrapping middleware in the | ||
/// [`Blueprint`]'s `middlewares` vector. | ||
pub(crate) middleware_id: usize, | ||
} | ||
|
||
impl<'a> WrappingMiddleware<'a> { | ||
#[track_caller] | ||
/// Register an error handler. | ||
/// | ||
/// Error handlers convert the error type returned by your middleware into an HTTP response. | ||
/// | ||
/// Error handlers **can't** consume the error type, they must take a reference to the | ||
/// error as input. | ||
/// Error handlers can have additional input parameters alongside the error, as long as there | ||
/// are constructors registered for those parameter types. | ||
/// | ||
/// ```rust | ||
/// use pavex::{f, blueprint::Blueprint, middleware::Next}; | ||
/// use pavex::{response::Response, hyper::body::Body}; | ||
/// use std::future::Future; | ||
/// # struct LogLevel; | ||
/// # struct Logger; | ||
/// # struct TimeoutError; | ||
/// | ||
/// fn timeout_middleware<C>(next: Next<C>) -> Result<Response, TimeoutError> | ||
/// where | ||
/// C: Future<Output = Response> | ||
/// { | ||
/// // [...] | ||
/// # todo!() | ||
/// } | ||
/// | ||
/// fn error_to_response(error: &TimeoutError, log_level: LogLevel) -> Response { | ||
/// // [...] | ||
/// # todo!() | ||
/// } | ||
/// | ||
/// # fn main() { | ||
/// let mut bp = Blueprint::new(); | ||
/// bp.wrap(f!(crate::timeout_middleware)) | ||
/// .error_handler(f!(crate::error_to_response)); | ||
/// # } | ||
/// ``` | ||
/// | ||
/// If an error handler has already been registered for the same error type, it will be | ||
/// overwritten. | ||
/// | ||
/// ## Common Errors | ||
/// | ||
/// Pavex will fail to generate the runtime code for your application if you register | ||
/// an error handler for an infallible middleware (i.e. a middleware that doesn't return | ||
/// a `Result`). | ||
pub fn error_handler(self, error_handler: RawCallable) -> Self { | ||
let callable_identifiers = RawCallableIdentifiers::from_raw_callable(error_handler); | ||
let callable = RegisteredCallable { | ||
callable: callable_identifiers, | ||
location: std::panic::Location::caller().into(), | ||
}; | ||
self.blueprint.middlewares[self.middleware_id].error_handler = Some(callable); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.