ntex_multipart/
error.rs

1//! Error and Result module
2use derive_more::{Display, From};
3use ntex::http::error::{DecodeError, PayloadError};
4use ntex::http::StatusCode;
5use ntex::web::error::{DefaultError, WebResponseError};
6
7/// A set of errors that can occur during parsing multipart streams
8#[derive(Debug, Display, From)]
9pub enum MultipartError {
10    /// Content-Type header is not found
11    #[display(fmt = "No Content-type header found")]
12    NoContentType,
13    /// Can not parse Content-Type header
14    #[display(fmt = "Can not parse Content-Type header")]
15    ParseContentType,
16    /// Multipart boundary is not found
17    #[display(fmt = "Multipart boundary is not found")]
18    Boundary,
19    /// Nested multipart is not supported
20    #[display(fmt = "Nested multipart is not supported")]
21    Nested,
22    /// Multipart stream is incomplete
23    #[display(fmt = "Multipart stream is incomplete")]
24    Incomplete,
25    /// Error during field parsing
26    #[display(fmt = "{}", _0)]
27    Decode(DecodeError),
28    /// Payload error
29    #[display(fmt = "{}", _0)]
30    Payload(PayloadError),
31    /// Not consumed
32    #[display(fmt = "Multipart stream is not consumed")]
33    NotConsumed,
34}
35
36impl std::error::Error for MultipartError {}
37
38/// Return `BadRequest` for `MultipartError`
39impl WebResponseError<DefaultError> for MultipartError {
40    fn status_code(&self) -> StatusCode {
41        StatusCode::BAD_REQUEST
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use ntex::web::test::TestRequest;
49    use ntex::web::HttpResponse;
50
51    #[test]
52    fn test_multipart_error() {
53        let req = TestRequest::default().to_http_request();
54        let resp: HttpResponse = MultipartError::Boundary.error_response(&req);
55        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
56    }
57}