Skip to content

Commit fffcb7e

Browse files
test: add generic error handler test (500)
Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
1 parent 1ed76b0 commit fffcb7e

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

.jules/bolt.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ The `heavyComputation` function was being called repeatedly with identical argum
2020

2121
Action:
2222
Implemented a `Map`-based cache for `heavyComputation` in `src/index.js`. Subsequent calls with the same `iterations` parameter now return the cached result in $O(1)$ time (~0.01ms), improving performance by several orders of magnitude for repeated inputs. Added unit tests in `tests/heavy_computation.test.js` to verify correctness and timing improvements.
23+
24+
## 2026-03-20 — Tested Generic Error Handler
25+
26+
Learning:
27+
The generic error handler in the API was returning a 500 status code but was not covered by any automated tests, leaving a gap in coverage for unexpected server-side failures.
28+
29+
Action:
30+
Added a test case in `tests/api.test.js` that intentionally mocks a failure in an internal dependency (`crypto.randomUUID`) to trigger the generic error handler. The test asserts that the handler returns a 500 status code and a sanitized JSON response (`{ error: 'Internal server error' }`) without leaking stack traces. Additionally, `console.error` was temporarily suppressed during the test to keep test output clean.

tests/api.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,31 @@ test('POST /v1/chat/completions fails with invalid JSON gracefully', async () =>
4545
assert.strictEqual(res.status, 400);
4646
assert.strictEqual(res.body.error, 'Invalid JSON payload');
4747
});
48+
49+
test('Generic error handler returns 500 without leaking stack traces', async () => {
50+
const crypto = require('crypto');
51+
const originalRandomUUID = crypto.randomUUID;
52+
const originalConsoleError = console.error;
53+
54+
// Mock internal failure
55+
crypto.randomUUID = () => { throw new Error('Mock internal failure'); };
56+
57+
// Suppress expected console.error from the error handler
58+
console.error = () => {};
59+
60+
try {
61+
const res = await request(app)
62+
.post('/v1/chat/completions')
63+
.send({
64+
model: 'gpt-4',
65+
messages: [{ role: 'user', content: 'Hello!' }]
66+
});
67+
68+
assert.strictEqual(res.status, 500);
69+
assert.deepStrictEqual(res.body, { error: 'Internal server error' });
70+
} finally {
71+
// Restore originals
72+
crypto.randomUUID = originalRandomUUID;
73+
console.error = originalConsoleError;
74+
}
75+
});

0 commit comments

Comments
 (0)