fix(api-express-router): honour errorStatusCode and JSON error envelope in default adapter#2720
Open
goastler wants to merge 5 commits into
Open
fix(api-express-router): honour errorStatusCode and JSON error envelope in default adapter#2720goastler wants to merge 5 commits into
goastler wants to merge 5 commits into
Conversation
…adapter
The default endpoint adapter catch block hardcoded
response.status(500).send("An internal server error occurred."),
ignoring the constructor's errorStatusCode (400 for the admin router) and
breaking the { error: ... } JSON envelope used by every other route.
ProsopoBaseErrors now surface their own status code via unwrapError with
the standard { error: jsonError } envelope; genuinely-unexpected errors
fall back to the configured errorStatusCode, still as JSON.
packages/api-express-router/src/endpointAdapter/apiExpressDefaultEndpointAdapter.ts
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the default Express endpoint adapter’s error handling so API errors preserve their intended HTTP status codes and return the standard { error: ... } JSON envelope instead of a hardcoded 500 with a plain-text body. This aligns the adapter’s behavior with the rest of the API router error formatting and allows consumers (e.g., admin router) to receive the configured errorStatusCode for unexpected failures.
Changes:
- Use
unwrapErrorto serializeProsopoBaseError-derived errors into{ error: jsonError }with the error’s status code. - For non-Prosopo errors, respond with
this.errorStatusCodeand a JSON error envelope (API.UNKNOWN) instead of500+ plain text.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…error envelope
- Only surface an error's own HTTP status when it actually carries one
(ProsopoApiError, or a base error with context.code); base errors
without a status code no longer leak as 400 and now map to the
configured errorStatusCode.
- Build the fallback envelope via a silent ProsopoApiError("API.UNKNOWN")
run through unwrapError so the message/key stay consistent and
localised instead of a hardcoded string.
- Add unit tests for the handleRequest error paths.
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.
What
In the default endpoint adapter's catch block, surface
ProsopoBaseErrors with their own status code viaunwrapError(standard{ error: jsonError }envelope), and fall back to the constructor-suppliederrorStatusCode(as JSON) for unexpected errors — instead of the hardcodedstatus(500).send("An internal server error occurred.").Why
The hardcoded 500 + plain-text body ignored the adapter's
errorStatusCode(set to 400 for the admin router instartProviderApi.ts~L310) and broke the{ error: ... }JSON envelope used by every other route, so admin client errors surfaced as 500 plain text.File:
packages/api-express-router/src/endpointAdapter/apiExpressDefaultEndpointAdapter.ts(~L54-80)Verification
npm run -w @prosopo/api-express-router build:tscyields the same 57 pre-existing errors with and without this change; the only error touching the edited file is a pre-existingCannot find module '@prosopo/logger'(project-reference build artifacts are absent in this checkout). No new type errors introduced.