Repository navigation
fix: catch unhandled request errors and return 500 (#23) - #76
Conversation
|
Thanks @Mohit-Gaur75, this is a useful safety net! On its own branch it's green: It now conflicts with Could you rebase on export function createApp() {
return http.createServer(async (request, response) => {
try { await handle(request, response); }
catch (error) {
console.error(error);
if (!response.headersSent) return json(response, 500, { error: 'Server error.' });
response.destroy();
}
});
}
async function handle(request, response) {
// ...the existing body from main, unchanged (including #75's /null/ block)...
}Your two tests should keep passing as they are. Ping me when it's pushed and I'll merge it. |
- Wrap createApp request listener in top-level try/catch
- Log unexpected errors with console.error
- Return JSON 500 { error: 'Server error.' } when headers have not been sent
- Destroy response socket if headers were already sent to prevent hung connections
- Add unit tests in test/server.test.js covering both error paths and server survival
Fixes blixvip#23
b398d7c to
4e0a2b9
Compare
|
@blixvip Rebased on |
|
Thanks @Mohit-Gaur75, this is merged! An unexpected error in a request now gets a clean 500 response and is logged, instead of crashing the whole server, so one bad request can't take the app down for everyone. Next step if you want another one: good first issue #77, "Point the docs at public/sections.mjs for section and beat timing" (#77). |
Summary of Changes
Addresses #23 by wrapping the request listener in
createApp()with a top-leveltry/catchblock:!response.headersSent), logs the error withconsole.errorand responds with HTTP status 500 JSON{ error: 'Server error.' }.response.destroy()to abort the broken stream and prevent hanging client sockets.test/server.test.jsvalidating:Fixes #23