actix_form_data/
error.rs

1/*
2 * This file is part of Actix Form Data.
3 *
4 * Copyright © 2020 Riley Trautman
5 *
6 * Actix Form Data is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Actix Form Data is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Actix Form Data.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20use 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}