GitHub Copilot prompt to add error handling
Error handling added without tests is just optimism with extra syntax. This prompt makes GitHub Copilot sweep a service directory for four specific failure patterns, route them through one typed boundary, and pair every fix with a test that forces the failure, so the handling is proven rather than assumed.
Last updated
Audit src/services/ for swallowed and unhandled errors, then fix and prove each fix with a test. Hunt for four patterns: awaited calls with no try around them in request handlers, catch blocks that only console.log, fetch responses used without checking res.ok, and JSON.parse on external input. Replace them with a small error hierarchy in src/errors.ts, AppError with an HTTP status and a code, plus NotFoundError and UpstreamError subclasses, and one boundary in the request pipeline that maps AppError to a response and everything else to a logged 500 with no stack in the body. Every fix gets a companion test in the matching .test file that forces the failure, a 404 from an upstream stub, malformed JSON, a rejected promise, and asserts the mapped status and code. Print a table of file, pattern found, fix applied. I will verify by reverting one fix locally and watching its companion test fail.
Same task in other tools
Questions about this prompt
Why one test per fix instead of a general error suite?
A paired test proves the failure path is reachable and mapped, and it pins the fix so a later refactor cannot quietly reintroduce the swallow. Reverting the fix should flip exactly one test red.
How does the boundary translate to a queue worker instead of HTTP?
Keep the AppError hierarchy and replace the response mapping with job handling: known codes decide retry versus dead-letter, unknown errors log the stack and dead-letter after a capped retry count.
Stack traces still leak into 500 responses in production. Where is the gap?
The catch-all branch is serializing the error object instead of logging it. The boundary must log the stack server-side and return only a status and generic code, so re-scope the prompt to that one file.