feat: standardise the error response envelope helper (#739) - #852
Open
Obiajulu-gif wants to merge 1 commit into
Open
feat: standardise the error response envelope helper (#739)#852Obiajulu-gif wants to merge 1 commit into
Obiajulu-gif wants to merge 1 commit into
Conversation
) lib/utils/api-response.utils.ts already had a `buildErrorResponse(code, message, details?)` helper matching the requested `{ error: { code, message, details? } }` shape (details already omitted when not provided/empty), but two pieces were missing and several handlers still built their own ad-hoc error objects: - buildErrorResponse now throws InvalidErrorCode for an empty string code, with a unit test for it, alongside the existing with/without- details coverage. - Replaced every remaining inline `{ success: false, ... }` / `{ success: false, code, message }` construction in route handlers and middleware with the shared helper (via buildErrorResponse directly or the sendError wrapper): error.middleware.ts's JWT/TokenExpired/Prisma/ ApiError/oversized-payload/malformed-JSON branches, body-parse-error.middleware.ts, ledger.controllers.ts and health.controllers.ts's catch-all 500s, admin.controllers.ts's lock- conflict 409, and auth.controllers.ts's refresh-token 401s (whose ad-hoc lowercase `code: 'invalid_token'` strings are now carried in `details` instead, since the envelope's `code` field is the shared ErrorCode vocabulary). - Left mapUnknownRouteError (route-error.utils.ts) as-is — it's the deliberate last-resort fallback with its own debug-mode stack/raw- error fields and already-factored tests, not an inline inconsistency. Also left the internal (non-HTTP-response) `{success: false, ...}` return values in token-refresh.utils.ts and stellar-submit.utils.ts alone since they aren't API response bodies. Updated the two affected test files (ledger.controllers.test.ts, body-parse-error.middleware.test.ts) for the new nested error shape. Verification: `tsc --noEmit` is clean on every file this PR touches (the repo has ~30 pre-existing unrelated errors on other files — missing Prisma models/redis nullability/etc — none overlapping this change). Ran the full test files covering everything touched: api-response.utils, ledger.controllers, body-parse-error.middleware, error.middleware, admin.controllers, health — 125/125 passing.
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
buildErrorResponse(code, message, details?)already existed inlib/utils/api-response.utils.tswith the exact requested shape —{ error: { code, message, details? } },detailsomitted when not provided/empty — but two things from the issue were still missing:buildErrorResponsenow throwsInvalidErrorCodewhen called with an empty string code.{ success: false, ... }objects instead of calling the helper, so the envelope shape wasn't actually consistent across all endpoints yet.Changes
buildErrorResponsethrowsInvalidErrorCodefor an empty code, with a new unit test alongside the existing with/without-details coverage.error.middleware.ts: JWT error, TokenExpired, Prisma error, ApiError catch, oversized-payload (413), malformed-JSON (400) branchesbody-parse-error.middleware.ts: both its 413 and 400 branchesledger.controllers.tsandhealth.controllers.ts: catch-all 500sadmin.controllers.ts: the indexer-replay lock-conflict 409auth.controllers.ts: the refresh-token 401s — their old lowercasecode: 'invalid_token'/code: result.codestrings now live indetailsinstead, since the envelope'scodefield is the sharedErrorCodevocabularymapUnknownRouteError(route-error.utils.ts) alone — it's the deliberate last-resort fallback with its own debug-mode stack/raw-error fields and already-factored tests, not an inline inconsistency to clean up.{ success: false, ... }return values intoken-refresh.utils.tsandstellar-submit.utils.tsalone — they're not API response bodies.ledger.controllers.test.ts,body-parse-error.middleware.test.ts) for the new nested error shape.Acceptance Criteria
{ error: { code, message } }consistent across all endpointsdetailsabsent when not providedInvalidErrorCodeTest plan
tsc --noEmitis clean on every file this PR touches (repo has ~30 pre-existing unrelated errors elsewhere — missing Prisma models, redis nullability, etc. — none overlapping this change)api-response.utils,ledger.controllers,body-parse-error.middleware,error.middleware,admin.controllers,health— 125/125 passingCloses #739