pavex/request/query/errors.rs
1//! Errors that can happen when extracting query parameters.
2
3use pavex_macros::methods;
4
5use crate::Response;
6
7/// The error returned by [`QueryParams::extract`] when the extraction fails.
8///
9/// See [`QueryParams::extract`] and the documentation of each error variant for more details.
10///
11/// Pavex provides [`ExtractQueryParamsError::into_response`] as the default error handler for
12/// this failure.
13///
14/// [`QueryParams::extract`]: crate::request::query::QueryParams::extract
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum ExtractQueryParamsError {
18 #[error(transparent)]
19 /// See [`QueryDeserializationError`] for details.
20 QueryDeserializationError(QueryDeserializationError),
21}
22
23#[methods]
24impl ExtractQueryParamsError {
25 /// Convert an [`ExtractQueryParamsError`] into an HTTP response.
26 ///
27 /// It returns a `400 Bad Request` to the caller.
28 #[error_handler(pavex = crate)]
29 pub fn into_response(&self) -> Response {
30 match self {
31 Self::QueryDeserializationError(e) => {
32 Response::bad_request().set_typed_body(format!("Invalid query parameters.\n{e:?}"))
33 }
34 }
35 }
36}
37
38#[derive(Debug, thiserror::Error)]
39#[error(transparent)]
40/// Something went wrong when trying to deserialize the percent-decoded query parameters into
41/// the target type you specified—`T` in [`QueryParams<T>`].
42///
43/// [`QueryParams<T>`]: crate::request::query::QueryParams
44pub struct QueryDeserializationError {
45 inner: serde_path_to_error::Error<serde_html_form::de::Error>,
46}
47
48impl QueryDeserializationError {
49 pub(super) fn new(e: serde_path_to_error::Error<serde_html_form::de::Error>) -> Self {
50 Self { inner: e }
51 }
52}