use std::fmt;
use http::StatusCode;
use serde::de::{self};
use crate::responder::Responder;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParamsError {
MissingPathParams,
DeserializationError(String),
}
impl std::fmt::Display for ParamsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingPathParams => write!(f, "path parameters not found in request extensions"),
Self::DeserializationError(err) => {
write!(f, "failed to deserialize path parameters: {err}")
}
}
}
}
impl std::error::Error for ParamsError {}
impl Responder for ParamsError {
fn into_response(self) -> crate::types::Response {
match self {
ParamsError::MissingPathParams => {
(StatusCode::INTERNAL_SERVER_ERROR, "Internal routing error").into_response()
}
ParamsError::DeserializationError(err) => (
StatusCode::BAD_REQUEST,
format!("Failed to deserialize path parameters: {err}"),
)
.into_response(),
}
}
}
#[derive(Debug)]
pub(crate) struct PathParamsDeError(String);
impl fmt::Display for PathParamsDeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for PathParamsDeError {}
impl de::Error for PathParamsDeError {
fn custom<T: fmt::Display>(msg: T) -> Self {
PathParamsDeError(msg.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn params_error_responder_status_codes() {
let resp = ParamsError::MissingPathParams.into_response();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
let resp = ParamsError::DeserializationError("bad".to_string()).into_response();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
}