2018-04-30 20:22:48 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Actix Form Data.
|
|
|
|
*
|
2020-06-06 02:40:44 +00:00
|
|
|
* Copyright © 2020 Riley Trautman
|
2018-04-30 20:22:48 +00:00
|
|
|
*
|
|
|
|
* 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,
|
|
|
|
};
|
2018-04-29 22:31:06 +00:00
|
|
|
|
2019-05-08 23:11:26 +00:00
|
|
|
use actix_multipart::MultipartError;
|
2018-10-09 23:35:10 +00:00
|
|
|
use actix_web::{
|
2019-05-08 23:11:26 +00:00
|
|
|
error::{PayloadError, ResponseError},
|
2020-06-06 02:40:44 +00:00
|
|
|
http::StatusCode,
|
2018-10-09 23:35:10 +00:00
|
|
|
HttpResponse,
|
|
|
|
};
|
2018-04-29 22:31:06 +00:00
|
|
|
|
2020-05-15 16:59:31 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2018-04-29 22:31:06 +00:00
|
|
|
pub enum Error {
|
2020-06-13 18:50:51 +00:00
|
|
|
#[error("{0}")]
|
2020-06-06 02:40:44 +00:00
|
|
|
FileFn(#[from] actix_web::Error),
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Error parsing payload, {0}")]
|
|
|
|
Payload(#[from] PayloadError),
|
|
|
|
#[error("Error in multipart creation, {0}")]
|
2019-05-08 23:11:26 +00:00
|
|
|
Multipart(MultipartError),
|
2020-05-15 16:59:31 +00:00
|
|
|
#[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")]
|
2018-04-29 22:31:06 +00:00
|
|
|
ContentType,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Bad Content-Disposition")]
|
2018-04-29 22:31:06 +00:00
|
|
|
ContentDisposition,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Failed to parse field name")]
|
2018-04-29 22:31:06 +00:00
|
|
|
Field,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Too many fields in request")]
|
2018-04-29 22:31:06 +00:00
|
|
|
FieldCount,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Field too large")]
|
2018-04-29 22:31:06 +00:00
|
|
|
FieldSize,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Found field with unexpected name or type")]
|
2018-04-29 22:31:06 +00:00
|
|
|
FieldType,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Failed to parse filename")]
|
2018-04-29 22:31:06 +00:00
|
|
|
Filename,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("Too many files in request")]
|
2018-04-29 22:31:06 +00:00
|
|
|
FileCount,
|
2020-05-15 16:59:31 +00:00
|
|
|
#[error("File too large")]
|
2018-04-29 22:31:06 +00:00
|
|
|
FileSize,
|
2020-06-06 02:40:44 +00:00
|
|
|
#[error("Uploaded guard used without Multipart middleware")]
|
|
|
|
MissingMiddleware,
|
|
|
|
#[error("Impossible Error! Middleware exists, didn't fail, and didn't send value")]
|
|
|
|
TxDropped,
|
2020-06-14 17:19:05 +00:00
|
|
|
#[error("Panic in spawned future")]
|
|
|
|
Panic,
|
2018-04-29 22:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MultipartError> for Error {
|
2020-05-15 16:59:31 +00:00
|
|
|
fn from(m: MultipartError) -> Self {
|
|
|
|
Error::Multipart(m)
|
2019-05-08 23:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:19:05 +00:00
|
|
|
impl From<crate::spawn::Canceled> for Error {
|
|
|
|
fn from(_: crate::spawn::Canceled) -> Self {
|
|
|
|
Error::Panic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:22:48 +00:00
|
|
|
impl ResponseError for Error {
|
2020-06-06 02:40:44 +00:00
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match *self {
|
|
|
|
Error::FileFn(ref e) => ResponseError::status_code(e.as_response_error()),
|
|
|
|
Error::Payload(ref e) => ResponseError::status_code(e),
|
|
|
|
Error::MissingMiddleware | Error::TxDropped => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
_ => StatusCode::BAD_REQUEST,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:22:48 +00:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
2020-06-06 02:40:44 +00:00
|
|
|
Error::FileFn(ref e) => ResponseError::error_response(e.as_response_error()),
|
2018-04-30 20:22:48 +00:00
|
|
|
Error::Payload(ref e) => ResponseError::error_response(e),
|
2020-05-15 16:59:31 +00:00
|
|
|
Error::Multipart(_)
|
|
|
|
| Error::ParseField(_)
|
|
|
|
| Error::ParseInt(_)
|
|
|
|
| Error::ParseFloat(_) => HttpResponse::BadRequest().finish(),
|
2018-04-30 20:22:48 +00:00
|
|
|
Error::ContentType
|
|
|
|
| Error::ContentDisposition
|
|
|
|
| Error::Field
|
|
|
|
| Error::FieldCount
|
|
|
|
| Error::FieldSize
|
|
|
|
| Error::FieldType
|
|
|
|
| Error::Filename
|
|
|
|
| Error::FileCount
|
|
|
|
| Error::FileSize => HttpResponse::BadRequest().finish(),
|
2020-06-14 17:19:05 +00:00
|
|
|
Error::Panic | Error::MissingMiddleware | Error::TxDropped => {
|
2020-06-06 02:40:44 +00:00
|
|
|
HttpResponse::InternalServerError().finish()
|
|
|
|
}
|
2018-04-30 20:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|