From bfdc29ebb8d93f5cc276ed32e81bed37bceb4fa6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 13 Mar 2023 14:22:50 +0000 Subject: [PATCH] normalize actix-files error messages --- actix-files/src/error.rs | 32 +++++++++++----------- actix-files/src/range.rs | 58 +++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/actix-files/src/error.rs b/actix-files/src/error.rs index 2f3a36cd1..68cea8d89 100644 --- a/actix-files/src/error.rs +++ b/actix-files/src/error.rs @@ -4,46 +4,44 @@ use derive_more::Display; /// Errors which can occur when serving static files. #[derive(Debug, PartialEq, Eq, Display)] pub enum FilesError { - /// Path is not a directory + /// Path is not a directory. #[allow(dead_code)] - #[display(fmt = "Path is not a directory. Unable to serve static files")] + #[display(fmt = "path is not a directory. Unable to serve static files")] IsNotDirectory, - /// Cannot render directory - #[display(fmt = "Unable to render directory without index file")] + /// Cannot render directory. + #[display(fmt = "unable to render directory without index file")] IsDirectory, } -/// Return `NotFound` for `FilesError` impl ResponseError for FilesError { + /// Returns `404 Not Found`. fn status_code(&self) -> StatusCode { StatusCode::NOT_FOUND } } -#[allow(clippy::enum_variant_names)] #[derive(Debug, PartialEq, Eq, Display)] #[non_exhaustive] pub enum UriSegmentError { - /// The segment started with the wrapped invalid character. - #[display(fmt = "The segment started with the wrapped invalid character")] + /// Segment started with the wrapped invalid character. + #[display(fmt = "segment started with invalid character: ('{_0}')")] BadStart(char), - /// The segment contained the wrapped invalid character. - #[display(fmt = "The segment contained the wrapped invalid character")] + /// Segment contained the wrapped invalid character. + #[display(fmt = "segment contained invalid character ('{_0}')")] BadChar(char), - /// The segment ended with the wrapped invalid character. - #[display(fmt = "The segment ended with the wrapped invalid character")] + /// Segment ended with the wrapped invalid character. + #[display(fmt = "segment ended with invalid character: ('{_0}')")] BadEnd(char), - - /// The path is not a valid UTF-8 string after doing percent decoding. - #[display(fmt = "The path is not a valid UTF-8 string after percent-decoding")] - NotValidUtf8, + // /// Path is not a valid UTF-8 string after percent-decoding. + // #[display(fmt = "path is not a valid UTF-8 string after percent-decoding")] + // NotValidUtf8, } -/// Return `BadRequest` for `UriSegmentError` impl ResponseError for UriSegmentError { + /// Returns `400 Bad Request`. fn status_code(&self) -> StatusCode { StatusCode::BAD_REQUEST } diff --git a/actix-files/src/range.rs b/actix-files/src/range.rs index 8d9fe9445..65c680ede 100644 --- a/actix-files/src/range.rs +++ b/actix-files/src/range.rs @@ -1,4 +1,36 @@ -use derive_more::{Display, Error}; +use std::fmt; + +use derive_more::Error; + +/// Copy of `http_range::HttpRangeParseError`. +#[derive(Debug, Clone)] +enum HttpRangeParseError { + InvalidRange, + NoOverlap, +} + +impl From for HttpRangeParseError { + fn from(err: http_range::HttpRangeParseError) -> Self { + match err { + http_range::HttpRangeParseError::InvalidRange => Self::InvalidRange, + http_range::HttpRangeParseError::NoOverlap => Self::NoOverlap, + } + } +} + +#[derive(Debug, Clone, Error)] +#[non_exhaustive] +pub struct ParseRangeErr(#[error(not(source))] HttpRangeParseError); + +impl fmt::Display for ParseRangeErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("invalid Range header: ")?; + f.write_str(match self.0 { + HttpRangeParseError::InvalidRange => "invalid syntax", + HttpRangeParseError::NoOverlap => "range starts after end of content", + }) + } +} /// HTTP Range header representation. #[derive(Debug, Clone, Copy)] @@ -10,26 +42,22 @@ pub struct HttpRange { pub length: u64, } -#[derive(Debug, Clone, Display, Error)] -#[display(fmt = "Parse HTTP Range failed")] -pub struct ParseRangeErr(#[error(not(source))] ()); - impl HttpRange { /// Parses Range HTTP header string as per RFC 2616. /// /// `header` is HTTP Range header (e.g. `bytes=bytes=0-9`). /// `size` is full size of response (file). pub fn parse(header: &str, size: u64) -> Result, ParseRangeErr> { - match http_range::HttpRange::parse(header, size) { - Ok(ranges) => Ok(ranges - .iter() - .map(|range| HttpRange { - start: range.start, - length: range.length, - }) - .collect()), - Err(_) => Err(ParseRangeErr(())), - } + let ranges = http_range::HttpRange::parse(header, size) + .map_err(|err| ParseRangeErr(err.into()))?; + + Ok(ranges + .iter() + .map(|range| HttpRange { + start: range.start, + length: range.length, + }) + .collect()) } }