use derive_more::{Display, Error, From};
use ntex::http::StatusCode;
use ntex::http::error::{DecodeError, PayloadError};
use ntex::rt::BlockingError;
use ntex::web::error::{DefaultError, WebResponseError};
#[derive(Debug, Display, From, Error)]
pub enum MultipartError {
#[display("No Content-type header found")]
NoContentType,
#[display("Can not parse Content-Type header")]
ParseContentType,
#[display("Parsed Content-Type did not have 'multipart' top-level media type")]
IncompatibleContentType,
#[display("Multipart boundary is not found")]
Boundary,
#[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
ContentDispositionMissing,
#[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
ContentDispositionNameMissing,
#[display("Nested multipart is not supported")]
Nested,
#[display("Multipart stream is incomplete")]
Incomplete,
#[display("{}", _0)]
Decode(DecodeError),
#[display("{}", _0)]
Payload(PayloadError),
#[display("Multipart stream is not consumed")]
NotConsumed,
#[display("An error occurred processing field: {}", name)]
Field { name: String, source: ntex::web::Error },
#[display("Duplicate field found: {}", _0)]
#[from(ignore)]
DuplicateField(#[error(not(source))] String),
#[display("Required field is missing: {}", _0)]
#[from(ignore)]
MissingField(#[error(not(source))] String),
#[display("Unknown field: {}", _0)]
#[from(ignore)]
UnknownField(#[error(not(source))] String),
Blocking(
#[error]
#[from]
BlockingError,
),
}
impl WebResponseError<DefaultError> for MultipartError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[cfg(test)]
mod tests {
use super::*;
use ntex::web::HttpResponse;
use ntex::web::test::TestRequest;
#[test]
fn test_multipart_error() {
let req = TestRequest::default().to_http_request();
let resp: HttpResponse = MultipartError::Boundary.error_response(&req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
}