-
-
Notifications
You must be signed in to change notification settings - Fork 11
fix(errors): pass an AppError to responses.error at the last two non-error sites #4066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PierreBrisorgueil
merged 3 commits into
master
from
fix-4064-responses-error-nonerror-arg
Sep 5, 2026
+211
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
323b095
fix(errors): pass an AppError to responses.error at the last two non-…
PierreBrisorgueil a780b7a
docs(analytics): add JSDoc to the requireFeatureFlag inner middleware
PierreBrisorgueil 3015073
docs(analytics): correct the inner middlewares @returns shape
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals'; | ||
|
|
||
| /** | ||
| * Unit tests for home.controller.js's `health` endpoint. | ||
| * | ||
| * Issue #4064: responses.error(res, status, title, description)(x) reads | ||
| * `x.details` off whatever it is handed. The degraded branch used to pass | ||
| * the raw health-check payload directly — not error-shaped, no `.details` | ||
| * key — the same wrong call convention fixed for | ||
| * analytics.requireFeatureFlag.js (this issue) and billing.requireQuota.js | ||
| * (#4062). No leak resulted (the payload carries nothing whitelisted), but | ||
| * the shape was wrong and a health payload is exactly the kind of object | ||
| * that accumulates internal detail over time. | ||
| */ | ||
| describe('home.controller health unit tests:', () => { | ||
| let health; | ||
| let mockHomeService; | ||
| let req; | ||
| let res; | ||
|
|
||
| beforeEach(async () => { | ||
| jest.resetModules(); | ||
|
|
||
| mockHomeService = { | ||
| getHealthStatus: jest.fn(), | ||
| }; | ||
|
|
||
| jest.unstable_mockModule('../services/home.service.js', () => ({ | ||
| default: mockHomeService, | ||
| })); | ||
|
|
||
| const mod = await import('../controllers/home.controller.js'); | ||
| health = mod.default.health; | ||
|
|
||
| req = { user: undefined }; | ||
| res = { | ||
| status: jest.fn().mockReturnThis(), | ||
| json: jest.fn().mockReturnThis(), | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| // Proven red against the pre-fix code: before this fix, the raw payload | ||
| // (no `.details` key) was handed straight to responses.error(...), so | ||
| // pickWhitelistedDetails always received `undefined` — this call shape | ||
| // could never have produced a non-error object's data as `payload.details`. | ||
| test('production mode: degraded response carries no payload.details today (no whitelisted key on the health payload), and never leaks the raw payload flat', async () => { | ||
| mockHomeService.getHealthStatus.mockReturnValue({ | ||
| status: 'degraded', | ||
| db: 'disconnected', | ||
| uptime: 12, | ||
| version: '1.0.0', | ||
| memory: { heapUsed: 1 }, | ||
| }); | ||
|
|
||
| const previousNodeEnv = process.env.NODE_ENV; | ||
| process.env.NODE_ENV = 'production'; | ||
| try { | ||
| health(req, res); | ||
| } finally { | ||
| process.env.NODE_ENV = previousNodeEnv; | ||
| } | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(503); | ||
| const payload = res.json.mock.calls[0][0]; | ||
| expect(payload.type).toBe('error'); | ||
| expect(payload.message).toBe('Service Unavailable'); | ||
| // No whitelisted key (type/upgradeUrl/retryAfter) exists on a health | ||
| // payload today, so payload.details stays absent — same observable | ||
| // behavior as before this fix. This proves the fix did not newly LEAK | ||
| // db/uptime/version/memory into payload.details. | ||
| expect(payload.details).toBeUndefined(); | ||
| // Production never leaks the raw serialized-error blob either way. | ||
| expect(payload.error).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('dev mode: the health payload is now nested under payload.error.details, not spread flat at payload.error\'s top level', async () => { | ||
| req.user = { roles: ['admin'] }; | ||
| mockHomeService.getHealthStatus.mockReturnValue({ | ||
| status: 'degraded', | ||
| db: 'disconnected', | ||
| uptime: 12, | ||
| version: '1.0.0', | ||
| memory: { heapUsed: 1 }, | ||
| }); | ||
|
|
||
| const previousNodeEnv = process.env.NODE_ENV; | ||
| process.env.NODE_ENV = 'test'; | ||
| try { | ||
| health(req, res); | ||
| } finally { | ||
| process.env.NODE_ENV = previousNodeEnv; | ||
| } | ||
|
|
||
| const payload = res.json.mock.calls[0][0]; | ||
| expect(typeof payload.error).toBe('string'); | ||
| const errorBlob = JSON.parse(payload.error); | ||
| // Shape change (dev-only, never seen in production): the health data now | ||
| // lives under `.details` (an AppError's shape) instead of being spread | ||
| // at the blob's top level. Any dev-only consumer parsing payload.error | ||
| // for `db`/`uptime`/`version`/`memory` directly must now read | ||
| // payload.error.details.<field> instead. | ||
| expect(errorBlob.db).toBeUndefined(); | ||
| expect(errorBlob.details).toEqual({ | ||
| status: 'degraded', | ||
| db: 'disconnected', | ||
| uptime: 12, | ||
| version: '1.0.0', | ||
| memory: { heapUsed: 1 }, | ||
| }); | ||
| }); | ||
|
|
||
| test('healthy status still returns 200 via responses.success, unaffected by this fix', async () => { | ||
| mockHomeService.getHealthStatus.mockReturnValue({ status: 'ok', db: 'connected' }); | ||
|
|
||
| health(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(200); | ||
| const payload = res.json.mock.calls[0][0]; | ||
| expect(payload.type).toBe('success'); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.