use actix_web::{
error::{ParseError, PayloadError},
http::StatusCode,
ResponseError,
};
use derive_more::{Display, Error, From};
#[derive(Debug, Display, From, Error)]
#[non_exhaustive]
pub enum MultipartError {
#[display(fmt = "No Content-Disposition `form-data` header")]
NoContentDisposition,
#[display(fmt = "No Content-Type header found")]
NoContentType,
#[display(fmt = "Can not parse Content-Type header")]
ParseContentType,
#[display(fmt = "Multipart boundary is not found")]
Boundary,
#[display(fmt = "Nested multipart is not supported")]
Nested,
#[display(fmt = "Multipart stream is incomplete")]
Incomplete,
#[display(fmt = "{}", _0)]
Parse(ParseError),
#[display(fmt = "{}", _0)]
Payload(PayloadError),
#[display(fmt = "Multipart stream is not consumed")]
NotConsumed,
#[display(
fmt = "An error occurred processing field `{}`: {}",
field_name,
source
)]
Field {
field_name: String,
source: actix_web::Error,
},
#[display(fmt = "Duplicate field found for: `{}`", _0)]
#[from(ignore)]
DuplicateField(#[error(not(source))] String),
#[display(fmt = "Field with name `{}` is required", _0)]
#[from(ignore)]
MissingField(#[error(not(source))] String),
#[display(fmt = "Unsupported field `{}`", _0)]
#[from(ignore)]
UnsupportedField(#[error(not(source))] String),
}
impl ResponseError for MultipartError {
fn status_code(&self) -> StatusCode {
match &self {
MultipartError::Field { source, .. } => source.as_response_error().status_code(),
_ => StatusCode::BAD_REQUEST,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_multipart_error() {
let resp = MultipartError::Boundary.error_response();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
}