1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-12 02:09:36 +00:00
actix-web/actix-http/src/error.rs

549 lines
16 KiB
Rust
Raw Normal View History

2019-03-26 18:54:35 +00:00
//! Error and Result module
use std::{
error::Error as StdError,
fmt,
io::{self, Write as _},
str::Utf8Error,
string::FromUtf8Error,
};
2019-03-26 18:54:35 +00:00
use bytes::BytesMut;
use derive_more::{Display, Error, From};
use http::{header, uri::InvalidUri, StatusCode};
2019-03-26 18:54:35 +00:00
use serde::de::value::Error as DeError;
use crate::{body::Body, helpers::Writer, Response};
2021-02-13 15:08:43 +00:00
pub use http::Error as HttpError;
2019-03-26 18:54:35 +00:00
/// General purpose actix web error.
///
/// An actix web error is used to carry errors from `std::error`
2019-03-26 18:54:35 +00:00
/// through actix in a convenient way. It can be created through
/// converting errors with `into()`.
///
/// Whenever it is created from an external object a response error is created
2021-02-11 22:39:54 +00:00
/// for it that can be used to create an HTTP response from it this means that
2019-03-26 18:54:35 +00:00
/// if you have access to an actix `Error` you can always get a
/// `ResponseError` reference from it.
pub struct Error {
2019-07-17 09:48:37 +00:00
cause: Box<dyn ResponseError>,
2019-03-26 18:54:35 +00:00
}
impl Error {
/// Returns the reference to the underlying `ResponseError`.
2019-07-17 09:48:37 +00:00
pub fn as_response_error(&self) -> &dyn ResponseError {
2019-03-26 18:54:35 +00:00
self.cause.as_ref()
}
/// Similar to `as_response_error` but downcasts.
pub fn as_error<T: ResponseError + 'static>(&self) -> Option<&T> {
2021-03-19 02:02:30 +00:00
<dyn ResponseError>::downcast_ref(self.cause.as_ref())
}
2019-03-26 18:54:35 +00:00
}
2021-04-19 20:12:52 +00:00
/// Errors that can generate responses.
2019-03-26 18:54:35 +00:00
pub trait ResponseError: fmt::Debug + fmt::Display {
2021-04-19 20:12:52 +00:00
/// Returns appropriate status code for error.
2019-03-26 18:54:35 +00:00
///
2021-04-19 20:12:52 +00:00
/// A 500 Internal Server Error is used by default. If [error_response](Self::error_response) is
/// also implemented and does not call `self.status_code()`, then this will not be used.
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
2019-03-26 18:54:35 +00:00
}
2021-04-19 20:12:52 +00:00
/// Creates full response for error.
2019-11-26 10:07:39 +00:00
///
2021-04-19 20:12:52 +00:00
/// By default, the generated response uses a 500 Internal Server Error status code, a
/// `Content-Type` of `text/plain`, and the body is set to `Self`'s `Display` impl.
2021-04-13 10:16:12 +00:00
fn error_response(&self) -> Response<Body> {
2019-11-26 10:07:39 +00:00
let mut resp = Response::new(self.status_code());
let mut buf = BytesMut::new();
let _ = write!(Writer(&mut buf), "{}", self);
resp.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("text/plain; charset=utf-8"),
);
resp.set_body(Body::from(buf))
}
downcast_get_type_id!();
}
downcast!(ResponseError);
2019-03-26 18:54:35 +00:00
impl fmt::Display for Error {
2019-12-07 18:46:51 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-03-26 18:54:35 +00:00
fmt::Display::fmt(&self.cause, f)
}
}
impl fmt::Debug for Error {
2019-12-07 18:46:51 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-12 01:05:39 +00:00
write!(f, "{:?}", &self.cause)
2019-03-26 18:54:35 +00:00
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
2019-12-05 17:35:43 +00:00
impl From<()> for Error {
fn from(_: ()) -> Self {
Error::from(UnitError)
}
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
// hint that an error that will never happen
unreachable!()
}
}
2019-03-26 18:54:35 +00:00
/// Convert `Error` to a `Response` instance
2021-04-13 10:16:12 +00:00
impl From<Error> for Response<Body> {
2019-03-26 18:54:35 +00:00
fn from(err: Error) -> Self {
Response::from_error(err)
}
}
/// `Error` for any error that implements `ResponseError`
impl<T: ResponseError + 'static> From<T> for Error {
fn from(err: T) -> Error {
Error {
cause: Box::new(err),
}
}
}
2021-04-14 05:07:59 +00:00
#[derive(Debug, Display, Error)]
#[display(fmt = "Unknown Error")]
2019-04-05 23:46:44 +00:00
struct UnitError;
impl ResponseError for Box<dyn StdError + 'static> {}
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`UnitError`].
2019-04-05 23:46:44 +00:00
impl ResponseError for UnitError {}
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`actix_tls::accept::openssl::SslError`].
#[cfg(feature = "openssl")]
impl ResponseError for actix_tls::accept::openssl::SslError {}
2019-04-11 22:12:23 +00:00
/// Returns [`StatusCode::BAD_REQUEST`] for [`DeError`].
2019-03-26 18:54:35 +00:00
impl ResponseError for DeError {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-03-26 18:54:35 +00:00
}
}
/// Returns [`StatusCode::BAD_REQUEST`] for [`Utf8Error`].
2019-03-26 18:54:35 +00:00
impl ResponseError for Utf8Error {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-03-26 18:54:35 +00:00
}
}
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`HttpError`].
2019-03-26 18:54:35 +00:00
impl ResponseError for HttpError {}
/// Inspects the underlying [`io::ErrorKind`] and returns an appropriate status code.
///
/// If the error is [`io::ErrorKind::NotFound`], [`StatusCode::NOT_FOUND`] is returned. If the
/// error is [`io::ErrorKind::PermissionDenied`], [`StatusCode::FORBIDDEN`] is returned. Otherwise,
/// [`StatusCode::INTERNAL_SERVER_ERROR`] is returned.
2019-03-26 18:54:35 +00:00
impl ResponseError for io::Error {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
2019-03-26 18:54:35 +00:00
match self.kind() {
2019-11-26 10:07:39 +00:00
io::ErrorKind::NotFound => StatusCode::NOT_FOUND,
io::ErrorKind::PermissionDenied => StatusCode::FORBIDDEN,
_ => StatusCode::INTERNAL_SERVER_ERROR,
2019-03-26 18:54:35 +00:00
}
}
}
/// Returns [`StatusCode::BAD_REQUEST`] for [`header::InvalidHeaderValue`].
2019-03-26 18:54:35 +00:00
impl ResponseError for header::InvalidHeaderValue {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-03-26 18:54:35 +00:00
}
}
2021-04-14 05:07:59 +00:00
/// A set of errors that can occur during parsing HTTP streams.
#[derive(Debug, Display, Error)]
#[non_exhaustive]
2019-03-26 18:54:35 +00:00
pub enum ParseError {
/// An invalid `Method`, such as `GE.T`.
#[display(fmt = "Invalid Method specified")]
Method,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// An invalid `Uri`, such as `exam ple.domain`.
#[display(fmt = "Uri error: {}", _0)]
Uri(InvalidUri),
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// An invalid `HttpVersion`, such as `HTP/1.1`
#[display(fmt = "Invalid HTTP version specified")]
Version,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// An invalid `Header`.
#[display(fmt = "Invalid Header provided")]
Header,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// A message head is too large to be reasonable.
#[display(fmt = "Message head is too large")]
TooLarge,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// A message reached EOF, but is not complete.
#[display(fmt = "Message is incomplete")]
Incomplete,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// An invalid `Status`, such as `1337 ELITE`.
#[display(fmt = "Invalid Status provided")]
Status,
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// A timeout occurred waiting for an IO event.
#[allow(dead_code)]
#[display(fmt = "Timeout")]
Timeout,
2021-04-14 05:07:59 +00:00
/// An `io::Error` that occurred while trying to read or write to a network stream.
2019-03-26 18:54:35 +00:00
#[display(fmt = "IO error: {}", _0)]
2019-03-28 18:08:24 +00:00
Io(io::Error),
2021-04-14 05:07:59 +00:00
2019-03-26 18:54:35 +00:00
/// Parsing a field as string failed
#[display(fmt = "UTF8 error: {}", _0)]
Utf8(Utf8Error),
}
/// Return `BadRequest` for `ParseError`
impl ResponseError for ParseError {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-03-26 18:54:35 +00:00
}
}
2019-03-28 18:08:24 +00:00
impl From<io::Error> for ParseError {
fn from(err: io::Error) -> ParseError {
2019-03-26 18:54:35 +00:00
ParseError::Io(err)
}
}
impl From<InvalidUri> for ParseError {
fn from(err: InvalidUri) -> ParseError {
ParseError::Uri(err)
}
}
impl From<Utf8Error> for ParseError {
fn from(err: Utf8Error) -> ParseError {
ParseError::Utf8(err)
}
}
impl From<FromUtf8Error> for ParseError {
fn from(err: FromUtf8Error) -> ParseError {
ParseError::Utf8(err.utf8_error())
}
}
impl From<httparse::Error> for ParseError {
fn from(err: httparse::Error) -> ParseError {
match err {
httparse::Error::HeaderName
| httparse::Error::HeaderValue
| httparse::Error::NewLine
| httparse::Error::Token => ParseError::Header,
httparse::Error::Status => ParseError::Status,
httparse::Error::TooManyHeaders => ParseError::TooLarge,
httparse::Error::Version => ParseError::Version,
}
}
}
/// A set of errors that can occur running blocking tasks in thread pool.
2021-04-14 05:07:59 +00:00
#[derive(Debug, Display, Error)]
#[display(fmt = "Blocking thread pool is gone")]
pub struct BlockingError;
/// `InternalServerError` for `BlockingError`
impl ResponseError for BlockingError {}
2021-04-14 05:07:59 +00:00
/// A set of errors that can occur during payload parsing.
#[derive(Debug, Display)]
#[non_exhaustive]
2019-03-26 18:54:35 +00:00
pub enum PayloadError {
/// A payload reached EOF, but is not complete.
2019-03-28 12:34:33 +00:00
#[display(
fmt = "A payload reached EOF, but is not complete. Inner error: {:?}",
2019-03-28 12:34:33 +00:00
_0
)]
2019-03-26 18:54:35 +00:00
Incomplete(Option<io::Error>),
/// Content encoding stream corruption.
2019-03-26 18:54:35 +00:00
#[display(fmt = "Can not decode content-encoding.")]
EncodingCorrupted,
/// Payload reached size limit.
#[display(fmt = "Payload reached size limit.")]
2019-03-26 18:54:35 +00:00
Overflow,
/// Payload length is unknown.
#[display(fmt = "Payload length is unknown.")]
2019-03-26 18:54:35 +00:00
UnknownLength,
/// HTTP/2 payload error.
2019-03-26 18:54:35 +00:00
#[display(fmt = "{}", _0)]
Http2Payload(h2::Error),
/// Generic I/O error.
2019-03-28 18:08:24 +00:00
#[display(fmt = "{}", _0)]
Io(io::Error),
}
impl std::error::Error for PayloadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
PayloadError::Incomplete(None) => None,
PayloadError::Incomplete(Some(err)) => Some(err as &dyn std::error::Error),
PayloadError::EncodingCorrupted => None,
PayloadError::Overflow => None,
PayloadError::UnknownLength => None,
PayloadError::Http2Payload(err) => Some(err as &dyn std::error::Error),
PayloadError::Io(err) => Some(err as &dyn std::error::Error),
}
}
}
2019-03-28 18:08:24 +00:00
impl From<h2::Error> for PayloadError {
fn from(err: h2::Error) -> Self {
PayloadError::Http2Payload(err)
}
}
impl From<Option<io::Error>> for PayloadError {
fn from(err: Option<io::Error>) -> Self {
PayloadError::Incomplete(err)
}
2019-03-26 18:54:35 +00:00
}
impl From<io::Error> for PayloadError {
fn from(err: io::Error) -> Self {
PayloadError::Incomplete(Some(err))
}
}
impl From<BlockingError> for PayloadError {
fn from(_: BlockingError) -> Self {
PayloadError::Io(io::Error::new(
io::ErrorKind::Other,
"Operation is canceled",
))
2019-03-28 18:08:24 +00:00
}
}
2019-03-26 18:54:35 +00:00
/// `PayloadError` returns two possible results:
///
/// - `Overflow` returns `PayloadTooLarge`
/// - Other errors returns `BadRequest`
impl ResponseError for PayloadError {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
2019-03-26 18:54:35 +00:00
match *self {
2019-11-26 10:07:39 +00:00
PayloadError::Overflow => StatusCode::PAYLOAD_TOO_LARGE,
_ => StatusCode::BAD_REQUEST,
2019-03-26 18:54:35 +00:00
}
}
}
2021-04-14 05:07:59 +00:00
/// A set of errors that can occur during dispatching HTTP requests.
#[derive(Debug, Display, Error, From)]
#[non_exhaustive]
2019-03-26 18:54:35 +00:00
pub enum DispatchError {
/// Service error
2019-04-05 23:46:44 +00:00
Service(Error),
2019-03-26 18:54:35 +00:00
/// Upgrade service error
Upgrade,
2019-03-26 18:54:35 +00:00
/// An `io::Error` that occurred while trying to read or write to a network
/// stream.
#[display(fmt = "IO error: {}", _0)]
Io(io::Error),
/// Http request parse error.
#[display(fmt = "Parse error: {}", _0)]
Parse(ParseError),
/// Http/2 error
#[display(fmt = "{}", _0)]
H2(h2::Error),
/// The first request did not complete within the specified timeout.
#[display(fmt = "The first request did not complete within the specified timeout")]
SlowRequestTimeout,
/// Disconnect timeout. Makes sense for ssl streams.
#[display(fmt = "Connection shutdown timeout")]
DisconnectTimeout,
/// Payload is not consumed
#[display(fmt = "Task is completed but request's payload is not consumed")]
PayloadIsNotConsumed,
/// Malformed request
#[display(fmt = "Malformed request")]
MalformedRequest,
/// Internal error
#[display(fmt = "Internal error")]
InternalError,
/// Unknown error
#[display(fmt = "Unknown error")]
Unknown,
}
2021-04-14 05:07:59 +00:00
/// A set of error that can occur during parsing content type.
#[derive(Debug, Display, Error)]
#[non_exhaustive]
2019-03-26 18:54:35 +00:00
pub enum ContentTypeError {
/// Can not parse content type
#[display(fmt = "Can not parse content type")]
ParseError,
2019-03-26 18:54:35 +00:00
/// Unknown content encoding
#[display(fmt = "Unknown content encoding")]
UnknownEncoding,
}
2021-04-14 05:07:59 +00:00
#[cfg(test)]
mod content_type_test_impls {
use super::*;
impl std::cmp::PartialEq for ContentTypeError {
fn eq(&self, other: &Self) -> bool {
match self {
Self::ParseError => matches!(other, ContentTypeError::ParseError),
Self::UnknownEncoding => {
matches!(other, ContentTypeError::UnknownEncoding)
}
}
}
}
}
2019-03-26 18:54:35 +00:00
impl ResponseError for ContentTypeError {
2019-11-26 10:07:39 +00:00
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
2019-03-26 18:54:35 +00:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{Error as HttpError, StatusCode};
use std::io;
#[test]
fn test_into_response() {
2021-04-13 10:16:12 +00:00
let resp: Response<Body> = ParseError::Incomplete.error_response();
2019-03-26 18:54:35 +00:00
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
2021-04-13 10:16:12 +00:00
let resp: Response<Body> = err.error_response();
2019-03-26 18:54:35 +00:00
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn test_as_response() {
let orig = io::Error::new(io::ErrorKind::Other, "other");
let e: Error = ParseError::Io(orig).into();
assert_eq!(format!("{}", e.as_response_error()), "IO error: other");
}
#[test]
fn test_error_cause() {
let orig = io::Error::new(io::ErrorKind::Other, "other");
let desc = orig.to_string();
2019-03-26 18:54:35 +00:00
let e = Error::from(orig);
assert_eq!(format!("{}", e.as_response_error()), desc);
}
#[test]
fn test_error_display() {
let orig = io::Error::new(io::ErrorKind::Other, "other");
let desc = orig.to_string();
2019-03-26 18:54:35 +00:00
let e = Error::from(orig);
assert_eq!(format!("{}", e), desc);
}
#[test]
fn test_error_http_response() {
let orig = io::Error::new(io::ErrorKind::Other, "other");
let e = Error::from(orig);
2021-04-13 10:16:12 +00:00
let resp: Response<Body> = e.into();
2019-03-26 18:54:35 +00:00
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
2019-03-28 12:04:39 +00:00
#[test]
fn test_payload_error() {
let err: PayloadError =
io::Error::new(io::ErrorKind::Other, "ParseError").into();
assert!(err.to_string().contains("ParseError"));
2019-03-28 12:04:39 +00:00
2019-03-28 12:34:33 +00:00
let err = PayloadError::Incomplete(None);
2019-03-28 12:04:39 +00:00
assert_eq!(
err.to_string(),
"A payload reached EOF, but is not complete. Inner error: None"
2019-03-28 12:04:39 +00:00
);
}
2019-03-26 18:54:35 +00:00
macro_rules! from {
($from:expr => $error:pat) => {
match ParseError::from($from) {
err @ $error => {
assert!(err.to_string().len() >= 5);
2019-03-26 18:54:35 +00:00
}
err => unreachable!("{:?}", err),
2019-03-26 18:54:35 +00:00
}
};
}
macro_rules! from_and_cause {
($from:expr => $error:pat) => {
match ParseError::from($from) {
e @ $error => {
let desc = format!("{}", e);
assert_eq!(desc, format!("IO error: {}", $from));
2019-03-26 18:54:35 +00:00
}
_ => unreachable!("{:?}", $from),
}
};
}
#[test]
fn test_from() {
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));
from!(httparse::Error::HeaderName => ParseError::Header);
from!(httparse::Error::HeaderName => ParseError::Header);
from!(httparse::Error::HeaderValue => ParseError::Header);
from!(httparse::Error::NewLine => ParseError::Header);
from!(httparse::Error::Status => ParseError::Status);
from!(httparse::Error::Token => ParseError::Header);
from!(httparse::Error::TooManyHeaders => ParseError::TooLarge);
from!(httparse::Error::Version => ParseError::Version);
}
#[test]
fn test_error_casting() {
let err = PayloadError::Overflow;
let resp_err: &dyn ResponseError = &err;
let err = resp_err.downcast_ref::<PayloadError>().unwrap();
assert_eq!(err.to_string(), "Payload reached size limit.");
let not_err = resp_err.downcast_ref::<ContentTypeError>();
assert!(not_err.is_none());
}
2019-03-26 18:54:35 +00:00
}