Skip to content

[4.x] Log errors without an error listener and send a 500 status - #158

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-execute-unhandled-error
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-execute-unhandled-error

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

execute() catches every Throwable from doExecute() (and compress()) and only dispatches application.error. When no dispatcher is set or nobody listens to that event, the error disappears: a doExecute() throwing RuntimeException('boom') results in HTTP/1.1 200 OK with an empty body and no log entry. Route-not-found, database errors and fatal Errors all become blank 200 responses.

This PR adds a protected AbstractApplication::dispatchErrorEvent(\Throwable $throwable): bool, used by both execute() implementations. It dispatches application.error as before; when no listener for it is registered (or no dispatcher is set) it logs the throwable via getLogger()->error() (with the throwable in the exception context key) and returns false. AbstractWebApplication::execute() then sets the status to 500 instead of reporting success.

Applications that register an error listener are not affected: the event is dispatched as before and the listener stays in charge of logging, status and body.

Behaviour change in 4.x: without an error listener, a failing web request now answers with status 500 instead of 200, and the throwable is passed to the logger. Rethrowing the throwable by default (the other option in the discussion) would be a breaking change and is left for 5.0.

Testing Instructions

New tests:

  • AbstractApplicationTest::testExecuteLogsErrorWithoutListener / testExecuteDispatchesErrorToListener
  • AbstractWebApplicationTest::testExecuteWithErrorAndNoListener / testExecuteWithErrorAndListener

The two "without listener" tests fail without the change in src/. Manual: let doExecute() throw in a WebApplication without a dispatcher; before the change the response is 200 OK, after it 500 Internal Server Error and the logger receives the error.

Documentation Changes Required

Updated: docs/lifecycle-and-events.md (Error handling), docs/overview.md ("No error page"), docs/cli-applications.md, docs/api-reference.md (execute() rows, new dispatchErrorEvent()), and the tutorial pages 07 and 08, which said that an error without a listener produces HTTP 200.

🤖 Generated with Claude Code

execute() catches every Throwable and only dispatches the application.error
event. Without a dispatcher or a listener for that event the error vanished:
a web application answered with "200 OK" and an empty body, and nothing was
logged.

The new dispatchErrorEvent() still dispatches the event, but logs the
throwable through the application logger when no listener is registered.
A web application then also sets the status to 500. Applications with an
error listener behave as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant