Skip to content

fix(utils): return 400 instead of 500 for invalid sort order query params#25751

Open
singhlovepreet9 wants to merge 1 commit intostrapi:developfrom
singhlovepreet9:fix/invalid-sort-order-400-error
Open

fix(utils): return 400 instead of 500 for invalid sort order query params#25751
singhlovepreet9 wants to merge 1 commit intostrapi:developfrom
singhlovepreet9:fix/invalid-sort-order-400-error

Conversation

@singhlovepreet9
Copy link
Copy Markdown

What does it do?

Changes InvalidOrderError and InvalidSortError in convert-query-params.ts to extend ValidationError (from @strapi/utils/errors) instead of the plain Error class.

This ensures Strapi's error middleware recognizes these as ApplicationError subclasses and returns a 400 status code with a descriptive error message, instead of a generic 500 internal server error.

Why is it needed?

When an invalid sort order suffix is provided in a query parameter (e.g., ?sort=field:asc$), Strapi throws an InvalidOrderError which extends plain Error. The error middleware only maps ApplicationError subclasses to 400-level responses — plain Error instances fall through to a 500 response.

This is inconsistent with how other invalid query parameters are handled (e.g., invalid pagination params already use PaginationError which extends ApplicationError and correctly returns 400).

How to test it?

  1. Create a content-type with some fields and add data
  2. Make an API request with a valid sort: GET /api/tests?sort=title:asc → works correctly
  3. Make an API request with an invalid sort order: GET /api/tests?sort=title:asc$
  4. Before fix: Returns 500 Internal Server Error
  5. After fix: Returns 400 with message "Invalid order. order can only be one of asc|desc|ASC|DESC"

Related issue(s)/PR(s)

Fix #25560

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Mar 16, 2026

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 16, 2026

@singhlovepreet9 is attempting to deploy a commit to the Strapi Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added the community Changes and fixes created by community members label Mar 16, 2026
@dosubot dosubot bot added pr: fix This PR is fixing a bug source: core:utils Source is core/utils or utils packages labels Mar 16, 2026
…rams

Invalid sort order suffixes (e.g., `sort=field:asc$`) threw a plain Error
resulting in a 500 response. Changed InvalidOrderError and InvalidSortError
to extend ValidationError so Strapi's error handler returns a proper 400.

Added tests for convertSortQueryParams covering valid orders, defaults,
multiple fields, and invalid order suffixes.

Fixes strapi#25560
@singhlovepreet9 singhlovepreet9 force-pushed the fix/invalid-sort-order-400-error branch from df9361c to 31bb93a Compare March 16, 2026 19:49
guoyangzhen added a commit to guoyangzhen/strapi that referenced this pull request Apr 1, 2026
…ust sort)

Several query parameter validators threw generic Error instead of
ValidationError, causing 500 instead of 400 responses.

Fixed:
- convertStartQueryParams: invalid start value
- convertLimitQueryParams: invalid limit value
- convertPopulateObject: invalid nested populate
- Invalid nested population query (polymorphic)
- Using fragments on non-morph attributes
- Invalid nested populate format
- Invalid fields parameter
- Invalid filters parameter

Complements strapi#25561 and strapi#25751 which fix sort-related errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Changes and fixes created by community members pr: fix This PR is fixing a bug source: core:utils Source is core/utils or utils packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid sort order suffix throws 500 rather than 400 error

1 participant