Skip to content

Handle non-AbortError errors in ErrorMiddleware - #4126

Open
MsfPablo wants to merge 2 commits into
SwiftPackageIndex:mainfrom
MsfPablo:fix-3944-error-middleware
Open

Handle non-AbortError errors in ErrorMiddleware#4126
MsfPablo wants to merge 2 commits into
SwiftPackageIndex:mainfrom
MsfPablo:fix-3944-error-middleware

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 7, 2026

Copy link
Copy Markdown

Refs #3944

Problem

ErrorMiddleware only catches AbortErrors. Any other error thrown from a route escapes the middleware, is converted into a raw 500 by Vapor's internal error handling, and is never logged by the application — so intermittent failures leave no trace.

This also explains the "An unknown error occurred" pages reported in #3944: swift-docc-render shows "page not found" for a 404 on a /data/…json request, but "An unknown error occurred" for any other error status. An unlogged raw 500 from a non-AbortError produces exactly that, with nothing in the logs to diagnose it.

(Credit to @DePasqualeOrg, who tracked this down in #3944 (comment).)

Change

ErrorMiddleware now catches every error, not just AbortError. Non-AbortErrors are treated as .internalServerError, logged at critical with the underlying error description, and rendered with the usual HTML error page. AbortErrors with a status below 400 keep propagating, as before.

The user-facing page only shows the generic "500 - Internal Server Error" text — the underlying error description goes to the log, not the response.

Notes

  • This is a visibility/robustness fix; it doesn't itself identify what is intermittently failing, but it makes the failure show up in the logs.
  • swift build passes locally. I couldn't run the test suite here (the toolchain in my environment can't resolve the Testing module for the test targets, which also fails on a clean checkout), so the added test is only verified by inspection — please let CI have the final word.

Errors that don't conform to AbortError escaped ErrorMiddleware and were
turned into a raw, unlogged 500 by Vapor's default error handling. That
makes intermittent failures invisible in the application logs and causes
swift-docc-render to display 'An unknown error occurred' rather than a
useful message.

Catch all errors, log them, and serve the standard HTML error page.

Refs SwiftPackageIndex#3944
@finestructure

Copy link
Copy Markdown
Member

Thanks for the PR, @MsfPablo ! We have a few high priority PRs that we need to take of at the moment but we'll be looking at this as soon as possible.

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