diff --git a/CHANGES.md b/CHANGES.md index 2a2e2e414..95f8b75e1 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 9f31582ed..a60276a7a 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 9ab98b17b..3bc067ab5 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")