actix-form-data/src/error.rs

97 lines
2.8 KiB
Rust
Raw Permalink Normal View History

/*
* This file is part of Actix Form Data.
*
2020-06-06 02:40:44 +00:00
* Copyright © 2020 Riley Trautman
*
* Actix Form Data is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Actix Form Data is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Actix Form Data. If not, see <http://www.gnu.org/licenses/>.
*/
2018-10-09 23:35:10 +00:00
use std::{
num::{ParseFloatError, ParseIntError},
string::FromUtf8Error,
};
use actix_multipart::MultipartError;
2018-10-09 23:35:10 +00:00
use actix_web::{
error::{PayloadError, ResponseError},
2020-06-06 02:40:44 +00:00
http::StatusCode,
2021-06-19 19:17:10 +00:00
HttpResponse,
2018-10-09 23:35:10 +00:00
};
#[derive(Debug, thiserror::Error)]
2021-09-14 01:45:09 +00:00
pub enum Error {
#[error("Error parsing payload, {0}")]
Payload(#[from] PayloadError),
#[error("Error in multipart creation, {0}")]
Multipart(MultipartError),
#[error("Failed to parse field, {0}")]
ParseField(#[from] FromUtf8Error),
#[error("Failed to parse int, {0}")]
ParseInt(#[from] ParseIntError),
#[error("Failed to parse float, {0}")]
ParseFloat(#[from] ParseFloatError),
#[error("Bad Content-Type")]
ContentType,
#[error("Bad Content-Disposition")]
ContentDisposition,
#[error("Failed to parse field name")]
Field,
#[error("Too many fields in request")]
FieldCount,
#[error("Field too large")]
FieldSize,
#[error("Found field with unexpected name or type")]
FieldType,
#[error("Failed to parse filename")]
Filename,
#[error("Too many files in request")]
FileCount,
#[error("File too large")]
FileSize,
}
2021-09-14 01:45:09 +00:00
impl From<MultipartError> for Error {
fn from(m: MultipartError) -> Self {
Error::Multipart(m)
}
}
2021-09-14 01:45:09 +00:00
impl ResponseError for Error {
2020-06-06 02:40:44 +00:00
fn status_code(&self) -> StatusCode {
match *self {
Error::Payload(ref e) => e.status_code(),
2020-06-06 02:40:44 +00:00
_ => StatusCode::BAD_REQUEST,
}
}
2021-06-19 19:17:10 +00:00
fn error_response(&self) -> HttpResponse {
match *self {
Error::Payload(ref e) => e.error_response(),
Error::Multipart(_)
| Error::ParseField(_)
| Error::ParseInt(_)
2021-06-19 19:17:10 +00:00
| Error::ParseFloat(_) => HttpResponse::BadRequest().finish(),
Error::ContentType
| Error::ContentDisposition
| Error::Field
| Error::FieldCount
| Error::FieldSize
| Error::FieldType
| Error::Filename
| Error::FileCount
2021-06-19 19:17:10 +00:00
| Error::FileSize => HttpResponse::BadRequest().finish(),
}
}
}