Skip to content

Add Symfony events for custom error responses - #8

Merged
rflavien merged 3 commits into
mainfrom
feature/custom-error-response
Mar 13, 2026
Merged

rflavien merged 3 commits into
mainfrom
feature/custom-error-response

Conversation

@rflavien

Copy link
Copy Markdown
Contributor

Summary

  • Add ApiSunsetEvent and ApiBrownoutEvent events dispatched before throwing GoneHttpException
  • Allow applications to set a custom Response on the event to override the default 410 behavior
  • Event dispatcher is optional — existing behavior preserved when absent
  • Enables structured JSON error responses (RFC 9457 Problem Details) for API consumers

Test plan

  • Custom response used when set on sunset event
  • Custom response used when set on brownout event
  • Falls back to GoneHttpException when no response set
  • Existing tests pass (null dispatcher)
  • PHPStan passes at max level

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings March 13, 2026 11:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Symfony event dispatching to the API deprecation bundle, allowing applications to customize error responses for sunset and brownout scenarios instead of always throwing GoneHttpException. Two new events (ApiSunsetEvent and ApiBrownoutEvent) are dispatched before the exception is thrown, and listeners can set a custom Response on the event to override the default 410 behavior.

Changes:

  • Added ApiDeprecationEvent base class and two concrete event classes (ApiSunsetEvent, ApiBrownoutEvent) for sunset and brownout scenarios
  • Modified DeprecationHeaderListener to optionally accept an EventDispatcherInterface and dispatch events before throwing GoneHttpException, replacing the controller with a custom response closure when one is provided
  • Wired the event_dispatcher service (with nullOnInvalid()) into the listener via the bundle configuration

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Event/ApiDeprecationEvent.php New abstract base event class holding the ApiDeprecated attribute, headers, and optional custom Response
src/Event/ApiSunsetEvent.php Concrete event class for sunset (post-sunset 410) scenarios
src/Event/ApiBrownoutEvent.php Concrete event class for brownout scenarios
src/EventListener/DeprecationHeaderListener.php Dispatches events before throwing GoneHttpException; uses custom response if set
src/ApiDeprecationBundle.php Injects optional event_dispatcher service into the listener
tests/EventListener/DeprecationHeaderListenerTest.php Four new tests covering custom response and fallback behavior for both event types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Dispatch ApiSunsetEvent and ApiBrownoutEvent before throwing GoneHttpException,
allowing applications to set a custom Response (e.g. RFC 9457 Problem Details)
instead of the default plain-text 410. Falls back to existing behavior when no
listener handles the event or no dispatcher is available.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rflavien
rflavien force-pushed the feature/custom-error-response branch from d55cb3f to ec46cd4 Compare March 13, 2026 14:29
@rflavien
rflavien merged commit 5cb12dc into main Mar 13, 2026
12 checks passed
@rflavien
rflavien deleted the feature/custom-error-response branch March 13, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants