Spring to NestJS Middleware and Interceptors: Filter ≠ Middleware

30 September 2026 nestjstypescriptnodejsjavaspring-boot
PDF The design sheet The full architecture and the verified references. Free, no email.
Thumbnail for Spring to NestJS Middleware and Interceptors: Filter ≠ Middleware 4:37
Watch on YouTube, or read on below.
Run this yourself → Clone the repo, docker compose up, and watch it happen

Every Spring developer has wrapped chain.doFilter in a try block. When the handler throws, the exception travels back up through that call and the filter catches it. Here it caught a ServletException wrapping the original IllegalStateException.

The same instinct in NestJS middleware catches nothing. Measured on 30 September 2026, on Node 22, NestJS 12 and Spring Boot 4.1.1:

GET /plain -> 500
  middleware: calling next()
  handler: throwing
  middleware: next() returned, headersSent=false
  middleware: finish, status 500

The handler runs inside next() and throws. next() still returns normally, and the 500 has not even been written yet. NestJS handles the exception inside its own route execution and writes the response afterwards. Awaiting next() changes nothing.

The hook that sees the error

One request, measured: middleware, guard, interceptor, pipe, handler, then back out through the interceptor. The exception filter is the escape path, taken only on a throw. An interceptor receives the handler result as a stream from next.handle(), so both the return value and the error flow through it:

return next.handle().pipe(
  map((data) => ({
    data,
    meta: {
      path: `${method} ${url}`,
      tookMs: Math.round(Number(process.hrtime.bigint() - started) / 1e6),
    },
  })),
);

The Spring name that misleads

HandlerInterceptor.postHandle looks like the after half. Measured, ResponseBodyAdvice ran first, and postHandle was handed no model at all. Spring’s own documentation points to ResponseBodyAdvice for reshaping a REST body, and it produced the same envelope as the NestJS interceptor. There is no single Spring class that maps to a NestJS interceptor: map capabilities, not names.

The full code for every measurement is in the repository, and the free design sheet above has the interceptor, the advice and the “which hook” 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.