Skip to content

Throw 404 EntityNotFoundException when query entity cannot be resolved#30

Merged
maltehuebner merged 1 commit into
mainfrom
fix/entity-not-found-404
Jun 12, 2026
Merged

Throw 404 EntityNotFoundException when query entity cannot be resolved#30
maltehuebner merged 1 commit into
mainfrom
fix/entity-not-found-404

Conversation

@maltehuebner

Copy link
Copy Markdown
Owner

Summary

  • Fixes a crash in ValueAssigner::assignEntityValueFromRepository(): the configured accessor was invoked on the repository lookup result before the null check. Any RequiredQueryParameter value that could not be resolved (e.g. GET /api/ride?citySlug=unknown-city on criticalmass.in) crashed with "Call to a member function getCity() on null" and surfaced as 500 Internal Server Error.
  • Without an accessor, a failed lookup previously skipped the filter silently, so an unknown slug returned the unfiltered entity list — wrong data instead of an error.
  • Both cases now throw the new EntityNotFoundException (extends DataQueryException, implements Symfony's HttpExceptionInterface with status 404). Consuming applications render a proper 404 Not Found automatically, no application-side changes required.
  • Adds test coverage for the whole entity-from-repository path (plain find, custom repository method, accessor — found, not found, accessor returns null) plus reusable fixtures (SimpleEntityQuery, SimpleEntityContainer, SimpleEntityContainerRepository).

Design notes

EntityNotFoundException implements HttpExceptionInterface instead of staying HTTP-agnostic so that Symfony's exception listener maps it to 404 out of the box. The bundle already targets Symfony applications (symfony/framework-bundle is a hard requirement) and the parameter resolution is inherently request-driven, mirroring how Doctrine's param converters throw NotFoundHttpException.

Test Plan

  • composer run tests — 361 tests, 534 assertions, green (5 new tests)
  • composer run phpstan — no errors (verified with platform PHP 8.3 / Symfony 7, matching CI)
  • After release: GET https://criticalmass.in/api/ride?citySlug=unknown returns 404 instead of 500

🤖 Generated with Claude Code

Previously, ValueAssigner::assignEntityValueFromRepository() called the
configured accessor on the repository lookup result before checking it
for null. An unresolvable required query parameter (e.g. an unknown
citySlug) therefore crashed with "Call to a member function on null",
surfacing as a 500 Internal Server Error in consuming applications.

Without an accessor the null lookup result silently skipped the filter,
returning the unfiltered entity list instead — equally wrong.

Now both cases throw the new EntityNotFoundException, which implements
Symfony's HttpExceptionInterface with status 404, so consuming
applications render a proper Not Found response without any changes.

Co-Authored-By: Claude Fable 5 <[email protected]>
@maltehuebner maltehuebner added bug Something isn't working AI-generated labels Jun 12, 2026
@maltehuebner maltehuebner merged commit 2cffbe4 into main Jun 12, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-generated bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant