Skip to content

fix: catch unhandled request errors and return 500 (#23) - #76

Merged
blixvip merged 1 commit into
blixvip:mainfrom
Mohit-Gaur75:fix/request-handler-error-handling
Nov 16, 2026
Merged

blixvip merged 1 commit into
blixvip:mainfrom
Mohit-Gaur75:fix/request-handler-error-handling

Conversation

@Mohit-Gaur75

Copy link
Copy Markdown
Contributor

Summary of Changes

Addresses #23 by wrapping the request listener in createApp() with a top-level try/catch block:

  • Error resilience: Catches any unhandled error that escapes during route processing so it does not result in an unhandled promise rejection that crashes the Node.js process.
  • Graceful response: If headers have not been sent (!response.headersSent), logs the error with console.error and responds with HTTP status 500 JSON { error: 'Server error.' }.
  • Socket cleanup: If headers were already sent when the error occurs, invokes response.destroy() to abort the broken stream and prevent hanging client sockets.
  • Server longevity: Ensures the server remains operational to serve subsequent requests.
  • Tests: Added unit tests in test/server.test.js validating:
    1. Unhandled route errors return a 500 JSON response and leave the server responsive.
    2. Errors occurring after headers are sent destroy the response stream and preserve server health.

Fixes #23

@blixvip

blixvip commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @Mohit-Gaur75, this is a useful safety net! On its own branch it's green: npm test passes 43/43, npm run check is clean, and both new tests do what they say (a 500 before headers are sent, a destroyed socket after, and the server stays up).

It now conflicts with main, though. #75 merged a few minutes ago and moved the /null/ prefix strip in server.js up, from after the /api/ guard to right after the 403 check. Your PR re-indents the whole handler, so git can't line the two up.

Could you rebase on main? One way to make it trivial, and to shrink the diff a lot, is to leave the handler body un-indented and wrap it from outside:

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
@Mohit-Gaur75
Mohit-Gaur75 force-pushed the fix/request-handler-error-handling branch from b398d7c to 4e0a2b9 Compare November 16, 2026 16:55
@Mohit-Gaur75

Copy link
Copy Markdown
Contributor Author

@blixvip Rebased on main and wrapped the handler with async function handle(request, response) as suggested. All 44 tests pass locally and npm run check is clean. Ready for merge!

@blixvip
blixvip merged commit d5b1636 into blixvip:main Nov 16, 2026
1 check passed
@blixvip

blixvip commented Nov 16, 2026

Copy link
Copy Markdown
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catch errors in the request handler so one bad request can't crash the server

2 participants