http_multipart/
error.rs

1use std::{error, fmt};
2
3#[derive(Debug)]
4pub enum MultipartError {
5    /// Only POST method is allowed for multipart.
6    NoPostMethod,
7    /// Content-Disposition header is not found or is not equal to "form-data".
8    NoContentDisposition,
9    /// Content-Type header is not found
10    NoContentType,
11    /// Can not parse Content-Type header
12    ParseContentType,
13    /// Multipart boundary is not found
14    Boundary,
15    /// Nested multipart is not supported
16    Nested,
17    /// Multipart stream is incomplete
18    UnexpectedEof,
19    /// Multipart parsing internal buffer overflown
20    BufferOverflow,
21    /// Error during header parsing
22    Header(httparse::Error),
23    /// Payload error
24    Payload(PayloadError),
25}
26
27pub type PayloadError = Box<dyn error::Error + Send + Sync>;
28
29impl fmt::Display for MultipartError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match *self {
32            Self::NoPostMethod => f.write_str("Only POST method is allowed for multipart"),
33            Self::NoContentDisposition => f.write_str("No Content-Disposition `form-data` header"),
34            Self::NoContentType => f.write_str("No Content-Type header found"),
35            Self::ParseContentType => f.write_str("Can not parse Content-Type header"),
36            Self::Boundary => f.write_str("Multipart boundary is not found"),
37            Self::Nested => f.write_str("Nested multipart is not supported"),
38            Self::UnexpectedEof => f.write_str("Multipart stream ended early than expected."),
39            Self::BufferOverflow => f.write_str("Multipart parsing internal buffer overflown"),
40            Self::Header(ref e) => fmt::Display::fmt(e, f),
41            Self::Payload(ref e) => fmt::Display::fmt(e, f),
42        }
43    }
44}
45
46impl error::Error for MultipartError {}
47
48impl From<httparse::Error> for MultipartError {
49    fn from(e: httparse::Error) -> Self {
50        Self::Header(e)
51    }
52}
53
54impl From<PayloadError> for MultipartError {
55    fn from(e: PayloadError) -> Self {
56        Self::Payload(e)
57    }
58}