use core::fmt;
use std::error;
#[derive(Debug)]
pub struct RequestError(InternalRequestError);
#[derive(Debug)]
pub(crate) enum InternalRequestError {
MissingHeader(&'static str),
InvalidContentType(String),
InvalidContentLength(std::num::ParseIntError),
ContentLengthMismatch { expected: usize, actual: usize },
}
impl From<InternalRequestError> for RequestError {
fn from(value: InternalRequestError) -> Self { RequestError(value) }
}
impl From<InternalRequestError> for super::ProtocolError {
fn from(e: InternalRequestError) -> Self { super::ProtocolError::V1(e.into()) }
}
impl fmt::Display for RequestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
InternalRequestError::MissingHeader(header) => write!(f, "Missing header: {header}"),
InternalRequestError::InvalidContentType(content_type) =>
write!(f, "Invalid content type: {content_type}"),
InternalRequestError::InvalidContentLength(e) => write!(f, "{e}"),
InternalRequestError::ContentLengthMismatch { expected, actual } =>
write!(f, "Content length mismatch: expected {expected}, got {actual}."),
}
}
}
impl error::Error for RequestError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.0 {
InternalRequestError::InvalidContentLength(e) => Some(e),
InternalRequestError::MissingHeader(_) => None,
InternalRequestError::InvalidContentType(_) => None,
InternalRequestError::ContentLengthMismatch { .. } => None,
}
}
}