Spring to NestJS Exception Filters: 500 or Process Exit?

30 September 2026 nestjstypescriptnodejsjavaspring-boot
PDF The design sheet The full architecture and the verified references. Free, no email.
Thumbnail for Spring to NestJS Exception Filters: 500 or Process Exit? 5:37
Watch on YouTube, or read on below.
Run this yourself → Clone the repo, docker compose up, and watch it happen

One line in a NestJS handler: a background write called with no await and no .catch(). The request returns normally. Then the write fails, and the whole Node process exits with code 1. At the time of recording, on Node 22, that is Node’s default for an unhandled rejection, and it has been since Node 15. It is not NestJS exception handling.

Everything below was measured on 30 September 2026, on Node 22, NestJS 12 and Spring Boot 4.1.1.

Inside a request, an error becomes a response

Thrown from a handler, all three shapes became responses on both stacks, and both apps kept serving:

ThrownNestJSSpring
a plain error500, generic500, generic
a domain exception, not translated500, generic500, generic
a framework not found404, message kept404, message dropped

For an error the framework does not recognise, the generic 500 is the safer default. But a client can act on insufficient funds, so it deserves a deliberate contract.

The filter, and the advice beside it

@Catch(InsufficientFunds)
class InsufficientFundsFilter implements ExceptionFilter<InsufficientFunds> {
  catch(exception: InsufficientFunds, host: ArgumentsHost) {
    const res = host.switchToHttp().getResponse();
    // 409: the account's current state prevents it. Not 402, still reserved.
    res.status(HttpStatus.CONFLICT).json({
      error: 'insufficient_funds',
      shortfallInMinorUnits: exception.shortfall,
      currency: exception.currency,
    });
  }
}

Registered with app.useGlobalFilters(new InsufficientFundsFilter()), it answers 409 with the shortfall and the currency. A @RestControllerAdvice with an @ExceptionHandler for the same exception answers with the same status and the same body. The concept transfers almost exactly; the difference is that Spring finds the advice by component scanning, while the NestJS filter is registered explicitly.

Give every promise an owner

The same failure, three ways, each one line different:

The filter was never broken. The error simply never reached it. Process-level hooks are for last-resort logging, not the fix.

The full code for every measurement is in the repository, and the free design sheet above has the filter, the advice and the checklist on one page.


Watch next

New videos land on Code with Sam - subscribe there if this was useful.

Get the interview pack

A free, practical pack for engineers preparing for senior backend interviews - the questions that actually get asked, and how to answer them like someone who has done the work.

New articles and videos when they land. No spam, and one click to leave.