Skip to main content

ntex_multipart/
error.rs

1//! Error and Result module
2use 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/// A set of errors that can occur during parsing multipart streams
9#[derive(Debug, Display, From, Error)]
10pub enum MultipartError {
11    /// Content-Type header is not found
12    #[display("No Content-type header found")]
13    NoContentType,
14
15    /// Can not parse Content-Type header
16    #[display("Can not parse Content-Type header")]
17    ParseContentType,
18
19    /// Parsed Content-Type did not have "multipart" top-level media type.
20    #[display("Parsed Content-Type did not have 'multipart' top-level media type")]
21    IncompatibleContentType,
22
23    /// Multipart boundary is not found
24    #[display("Multipart boundary is not found")]
25    Boundary,
26
27    /// Content-Disposition header was not found or not of disposition type "form-data" when parsing
28    /// a "form-data" field.
29    #[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
30    ContentDispositionMissing,
31
32    /// Content-Disposition name parameter was not found when parsing a "form-data" field.
33    #[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
34    ContentDispositionNameMissing,
35
36    /// Nested multipart is not supported
37    #[display("Nested multipart is not supported")]
38    Nested,
39
40    /// Multipart stream is incomplete
41    #[display("Multipart stream is incomplete")]
42    Incomplete,
43
44    /// Error during field parsing
45    #[display("{}", _0)]
46    Decode(DecodeError),
47
48    /// Payload error
49    #[display("{}", _0)]
50    Payload(PayloadError),
51
52    /// Not consumed
53    #[display("Multipart stream is not consumed")]
54    NotConsumed,
55
56    /// Form field handler raised error.
57    #[display("An error occurred processing field: {}", name)]
58    Field { name: String, source: ntex::web::Error },
59
60    /// Duplicate field found (for structure that opted-in to denying duplicate fields).
61    #[display("Duplicate field found: {}", _0)]
62    #[from(ignore)]
63    DuplicateField(#[error(not(source))] String),
64
65    /// Required field is missing.
66    #[display("Required field is missing: {}", _0)]
67    #[from(ignore)]
68    MissingField(#[error(not(source))] String),
69
70    /// Unknown field (for structure that opted-in to denying unknown fields).
71    #[display("Unknown field: {}", _0)]
72    #[from(ignore)]
73    UnknownField(#[error(not(source))] String),
74
75    /// Blocking error
76    Blocking(
77        #[error]
78        #[from]
79        BlockingError,
80    ),
81}
82
83/// Return `BadRequest` for `MultipartError`
84impl 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}