From 32a1c365975acffb25959f5887a6530396788504 Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Tue, 17 Sep 2019 02:58:04 +0200 Subject: [PATCH] Make UrlencodedError::Overflow more informative (#1089) --- CHANGES.md | 1 + src/error.rs | 13 +++++++++---- src/types/form.rs | 16 +++++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2a2e2e41..95f8b75e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ * Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload` +* Make UrlEncodedError::Overflow more informativve ## [1.0.7] - 2019-08-29 diff --git a/src/error.rs b/src/error.rs index 9f31582e..a60276a7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,8 +32,12 @@ pub enum UrlencodedError { #[display(fmt = "Can not decode chunked transfer encoding")] Chunked, /// Payload size is bigger than allowed. (default: 256kB) - #[display(fmt = "Urlencoded payload size is bigger than allowed (default: 256kB)")] - Overflow, + #[display( + fmt = "Urlencoded payload size is bigger ({} bytes) than allowed (default: {} bytes)", + size, + limit + )] + Overflow { size: usize, limit: usize }, /// Payload size is now known #[display(fmt = "Payload size is now known")] UnknownLength, @@ -52,7 +56,7 @@ pub enum UrlencodedError { impl ResponseError for UrlencodedError { fn error_response(&self) -> HttpResponse { match *self { - UrlencodedError::Overflow => { + UrlencodedError::Overflow { .. } => { HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE) } UrlencodedError::UnknownLength => { @@ -164,7 +168,8 @@ mod tests { #[test] fn test_urlencoded_error() { - let resp: HttpResponse = UrlencodedError::Overflow.error_response(); + let resp: HttpResponse = + UrlencodedError::Overflow { size: 0, limit: 0 }.error_response(); assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); let resp: HttpResponse = UrlencodedError::UnknownLength.error_response(); assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED); diff --git a/src/types/form.rs b/src/types/form.rs index 9ab98b17..3bc067ab 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -318,7 +318,7 @@ where let limit = self.limit; if let Some(len) = self.length.take() { if len > limit { - return Err(UrlencodedError::Overflow); + return Err(UrlencodedError::Overflow { size: len, limit }); } } @@ -331,7 +331,10 @@ where .from_err() .fold(BytesMut::with_capacity(8192), move |mut body, chunk| { if (body.len() + chunk.len()) > limit { - Err(UrlencodedError::Overflow) + Err(UrlencodedError::Overflow { + size: body.len() + chunk.len(), + limit, + }) } else { body.extend_from_slice(&chunk); Ok(body) @@ -390,8 +393,8 @@ mod tests { fn eq(err: UrlencodedError, other: UrlencodedError) -> bool { match err { - UrlencodedError::Overflow => match other { - UrlencodedError::Overflow => true, + UrlencodedError::Overflow { .. } => match other { + UrlencodedError::Overflow { .. } => true, _ => false, }, UrlencodedError::UnknownLength => match other { @@ -420,7 +423,10 @@ mod tests { .header(CONTENT_LENGTH, "1000000") .to_http_parts(); let info = block_on(UrlEncoded::::new(&req, &mut pl)); - assert!(eq(info.err().unwrap(), UrlencodedError::Overflow)); + assert!(eq( + info.err().unwrap(), + UrlencodedError::Overflow { size: 0, limit: 0 } + )); let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain") .header(CONTENT_LENGTH, "10")