Skip to content

Commit d0387ad

Browse files
committed
Convert pagination ISE into a Bad Request response
Errors in parsing the pagination query string parameters can be returned to the client and do not need to propagate up to the logging middleware. The following have been observed in production logs: * error="cannot parse integer from empty string" for `/api/v1/crates?page=&per_page=50&sort=downloads` * error="invalid digit found in string" for `/api/v1/crates?page=1&per_page=100%22%EF%BC%8Cexception"`
1 parent a251368 commit d0387ad

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/controllers/helpers/pagination.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) enum Page {
1616
impl Page {
1717
fn new(params: &IndexMap<String, String>) -> AppResult<Self> {
1818
if let Some(s) = params.get("page") {
19-
let numeric_page = s.parse()?;
19+
let numeric_page = s.parse().map_err(|e| bad_request(&e))?;
2020
if numeric_page < 1 {
2121
return Err(cargo_err(&format_args!(
2222
"page indexing starts from 1, page {} is invalid",
@@ -44,7 +44,7 @@ impl PaginationOptions {
4444

4545
let per_page = params
4646
.get("per_page")
47-
.map(|s| s.parse())
47+
.map(|s| s.parse().map_err(|e| bad_request(&e)))
4848
.unwrap_or(Ok(DEFAULT_PER_PAGE))?;
4949

5050
if per_page > MAX_PER_PAGE {

0 commit comments

Comments
 (0)