1use derive_more::{Display, Error, From};
3use ntex::http::StatusCode;
4use ntex::http::error::{DecodeError, PayloadError};
5use ntex::rt::BlockingError;
6use ntex::web::error::{DefaultError, WebResponseError};
7
8#[derive(Debug, Display, From, Error)]
10pub enum MultipartError {
11 #[display("No Content-type header found")]
13 NoContentType,
14
15 #[display("Can not parse Content-Type header")]
17 ParseContentType,
18
19 #[display("Parsed Content-Type did not have 'multipart' top-level media type")]
21 IncompatibleContentType,
22
23 #[display("Multipart boundary is not found")]
25 Boundary,
26
27 #[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
30 ContentDispositionMissing,
31
32 #[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
34 ContentDispositionNameMissing,
35
36 #[display("Nested multipart is not supported")]
38 Nested,
39
40 #[display("Multipart stream is incomplete")]
42 Incomplete,
43
44 #[display("{}", _0)]
46 Decode(DecodeError),
47
48 #[display("{}", _0)]
50 Payload(PayloadError),
51
52 #[display("Multipart stream is not consumed")]
54 NotConsumed,
55
56 #[display("An error occurred processing field: {}", name)]
58 Field { name: String, source: ntex::web::Error },
59
60 #[display("Duplicate field found: {}", _0)]
62 #[from(ignore)]
63 DuplicateField(#[error(not(source))] String),
64
65 #[display("Required field is missing: {}", _0)]
67 #[from(ignore)]
68 MissingField(#[error(not(source))] String),
69
70 #[display("Unknown field: {}", _0)]
72 #[from(ignore)]
73 UnknownField(#[error(not(source))] String),
74
75 Blocking(
77 #[error]
78 #[from]
79 BlockingError,
80 ),
81}
82
83impl WebResponseError<DefaultError> for MultipartError {
85 fn status_code(&self) -> StatusCode {
86 StatusCode::BAD_REQUEST
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93 use ntex::web::HttpResponse;
94 use ntex::web::test::TestRequest;
95
96 #[test]
97 fn test_multipart_error() {
98 let req = TestRequest::default().to_http_request();
99 let resp: HttpResponse = MultipartError::Boundary.error_response(&req);
100 assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
101 }
102}