1use std::{
21 num::{ParseFloatError, ParseIntError},
22 string::FromUtf8Error,
23};
24
25use actix_multipart::MultipartError;
26use actix_web::{
27 error::{PayloadError, ResponseError},
28 http::StatusCode,
29 HttpResponse,
30};
31
32#[derive(Debug, thiserror::Error)]
33pub enum Error {
34 #[error("Error parsing payload, {0}")]
35 Payload(#[from] PayloadError),
36 #[error("Error in multipart creation, {0}")]
37 Multipart(MultipartError),
38 #[error("Failed to parse field, {0}")]
39 ParseField(#[from] FromUtf8Error),
40 #[error("Failed to parse int, {0}")]
41 ParseInt(#[from] ParseIntError),
42 #[error("Failed to parse float, {0}")]
43 ParseFloat(#[from] ParseFloatError),
44 #[error("Bad Content-Type")]
45 ContentType,
46 #[error("Bad Content-Disposition")]
47 ContentDisposition,
48 #[error("Failed to parse field name")]
49 Field,
50 #[error("Too many fields in request")]
51 FieldCount,
52 #[error("Field too large")]
53 FieldSize,
54 #[error("Found field with unexpected name or type")]
55 FieldType,
56 #[error("Failed to parse filename")]
57 Filename,
58 #[error("Too many files in request")]
59 FileCount,
60 #[error("File too large")]
61 FileSize,
62}
63
64impl From<MultipartError> for Error {
65 fn from(m: MultipartError) -> Self {
66 Error::Multipart(m)
67 }
68}
69
70impl ResponseError for Error {
71 fn status_code(&self) -> StatusCode {
72 match *self {
73 Error::Payload(ref e) => e.status_code(),
74 _ => StatusCode::BAD_REQUEST,
75 }
76 }
77
78 fn error_response(&self) -> HttpResponse {
79 match *self {
80 Error::Payload(ref e) => e.error_response(),
81 Error::Multipart(_)
82 | Error::ParseField(_)
83 | Error::ParseInt(_)
84 | Error::ParseFloat(_) => HttpResponse::BadRequest().finish(),
85 Error::ContentType
86 | Error::ContentDisposition
87 | Error::Field
88 | Error::FieldCount
89 | Error::FieldSize
90 | Error::FieldType
91 | Error::Filename
92 | Error::FileCount
93 | Error::FileSize => HttpResponse::BadRequest().finish(),
94 }
95 }
96}