Skip to content

HIGH: API Gateway has no global Express error handler or graceful shutdown #44

@Senthil455

Description

@Senthil455

Summary

The API Gateway has no global Express error handler. Async middleware rejections become unhandled promise rejections, crashing the process in Node.js 20+.

Affected File

services/api-gateway-node/index.js

Root Cause

Express 5 requires a global error handler middleware to catch errors from async route handlers and middleware. Without it:

// Missing:
app.use((err, req, res, next) => {
    console.error('Unhandled error:', err);
    res.status(500).json({ error: 'Internal server error' });
});

Any synchronous throw or async rejection in route handlers or middleware will:

  1. Crash the process (Node.js 20+ terminates on unhandled rejections)
  2. Return a generic HTML error page instead of JSON
  3. Leak stack traces to the client

Additionally, there is no graceful shutdown:

// Missing:
process.on('SIGTERM', () => {
    server.close(() => process.exit(0));
});

Impact

  • Any unhandled error crashes the entire API Gateway
  • All downstream services become unreachable
  • Unhandled promise rejections terminate the Node.js process
  • In production, this causes cascading failures across the entire platform

Fix Required

  1. Add a global Express error handler:
app.use((err, req, res, next) => {
    logger.error('unhandled_error', { error: err.message, stack: err.stack });
    res.status(err.status || 500).json({ error: 'Internal server error' });
});
  1. Add graceful shutdown handlers for SIGTERM/SIGINT
  2. Use express-async-errors or ensure all async handlers catch errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghighHigh severity

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions