1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Errors that can happen when extracting query parameters.

use crate::response::Response;

/// The error returned by [`QueryParams::extract`] when the extraction fails.
///
/// See [`QueryParams::extract`] and the documentation of each error variant for more details.
///
/// Pavex provides [`ExtractQueryParamsError::into_response`] as the default error handler for
/// this failure.
///
/// [`QueryParams::extract`]: crate::request::query::QueryParams::extract
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExtractQueryParamsError {
    #[error(transparent)]
    /// See [`QueryDeserializationError`] for details.
    QueryDeserializationError(QueryDeserializationError),
}

impl ExtractQueryParamsError {
    /// Convert an [`ExtractQueryParamsError`] into an HTTP response.
    ///
    /// It returns a `400 Bad Request` to the caller.
    pub fn into_response(&self) -> Response {
        match self {
            Self::QueryDeserializationError(e) => Response::bad_request()
                .set_typed_body(format!("Invalid query parameters.\n{:?}", e)),
        }
    }
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
/// Something went wrong when trying to deserialize the percent-decoded query parameters into
/// the target type you specified—`T` in [`QueryParams<T>`].
///
/// [`QueryParams<T>`]: crate::request::query::QueryParams
pub struct QueryDeserializationError {
    inner: serde_html_form::de::Error,
}

impl QueryDeserializationError {
    pub(super) fn new(e: serde_html_form::de::Error) -> Self {
        Self { inner: e }
    }
}