fix(server): route middleware GraphQL errors through the error registry - #1540
Merged
Conversation
A stale session rendered in the UI as a blank `GraphQL Error:` — the auth
middleware answered with `{errors:[{extensions:{code:'UNAUTHENTICATED'}}]}`
and no top-level `message`, which clients join into an empty string.
- auth/captcha middleware now emit registry-backed errors via a shared
respondWithGraphQLError helper
- the auth failure path no longer returns the raw Postgres error text to
clients (dev-only detail via INTERNAL_FAILURE)
- register CAPTCHA_REQUIRED / CAPTCHA_FAILED so no middleware needs literals
- generated ORM client falls back to extensions.code when message is absent
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A stale session token rendered in the Platform UI as a bare
GraphQL Error:with nothing after it. The cause is not the error registry —@constructive-io/errorsdefinesUNAUTHENTICATEDcorrectly (public/401/"You must be signed in to do that.") andConstructiveError.toExtensions()already produces a proper payload. The cause is thatmiddleware/auth.tsbypassed the registry and hand-built the response body:With no top-level
message, every client that doeserrors.map(e => e.message).join('; ')renders an empty string.The same file's catch branch was worse: it used an unregistered code (
BAD_TOKEN_DEFINITION) and put the raw Postgres error text inextensions.message— invisible to clients and an internals leak. It now raisesINTERNAL_FAILURE, with the database detail included only in development.To stop this class of bug rather than the one instance, the response shape is now built in exactly one place:
captcha.ts— the only other middleware hand-writing GraphQL error payloads — is routed through it too, which required registeringCAPTCHA_REQUIREDandCAPTCHA_FAILEDso no middleware needs string literals for codes.Finally, defense on the client side: the generated ORM client falls back to
extensions.codewhen a server omitsmessage, so a message-less error can degrade toGraphQL Error: UNAUTHENTICATEDinstead ofGraphQL Error:.Testing
graphql/server: newerrors/__tests__/graphql-response.test.ts(3 tests) asserting a top-level message, HTTP 200, and context interpolation; existing middleware suites 60/60.graphql/codegen: new test constructingGraphQLRequestErrorfrom a message-less error and asserting the code fallback; suite 11/11 with the client snapshot updated.tsc --noEmitclean forpackages/errors,graphql/server,graphql/codegen.Note:
pnpm lintfails repo-wide before this change — ESLint 9 requires a flat config and the repo still ships.eslintrc.json. Unrelated to this diff, but worth fixing separately.Link to Devin session: https://app.devin.ai/sessions/4bc483ff6d644a7b9a2fed56af9a2111
Requested by: @pyramation