2019-04-03 21:28:58 +02:00
|
|
|
//! Error and Result module
|
|
|
|
use actix_web::error::{ParseError, PayloadError};
|
|
|
|
use actix_web::http::StatusCode;
|
|
|
|
use actix_web::{HttpResponse, ResponseError};
|
|
|
|
use derive_more::{Display, From};
|
|
|
|
|
|
|
|
/// A set of errors that can occur during parsing multipart streams
|
|
|
|
#[derive(Debug, Display, From)]
|
|
|
|
pub enum MultipartError {
|
|
|
|
/// Content-Type header is not found
|
|
|
|
#[display(fmt = "No Content-type header found")]
|
|
|
|
NoContentType,
|
|
|
|
/// Can not parse Content-Type header
|
|
|
|
#[display(fmt = "Can not parse Content-Type header")]
|
|
|
|
ParseContentType,
|
|
|
|
/// Multipart boundary is not found
|
|
|
|
#[display(fmt = "Multipart boundary is not found")]
|
|
|
|
Boundary,
|
2019-04-13 19:11:07 +02:00
|
|
|
/// Nested multipart is not supported
|
|
|
|
#[display(fmt = "Nested multipart is not supported")]
|
|
|
|
Nested,
|
2019-04-03 21:28:58 +02:00
|
|
|
/// Multipart stream is incomplete
|
|
|
|
#[display(fmt = "Multipart stream is incomplete")]
|
|
|
|
Incomplete,
|
|
|
|
/// Error during field parsing
|
|
|
|
#[display(fmt = "{}", _0)]
|
|
|
|
Parse(ParseError),
|
|
|
|
/// Payload error
|
|
|
|
#[display(fmt = "{}", _0)]
|
|
|
|
Payload(PayloadError),
|
2019-05-12 20:43:05 +02:00
|
|
|
/// Not consumed
|
|
|
|
#[display(fmt = "Multipart stream is not consumed")]
|
|
|
|
NotConsumed,
|
2019-04-03 21:28:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `BadRequest` for `MultipartError`
|
|
|
|
impl ResponseError for MultipartError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
HttpResponse::new(StatusCode::BAD_REQUEST)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multipart_error() {
|
|
|
|
let resp: HttpResponse = MultipartError::Boundary.error_response();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
}
|