demo_app/server/src/main/java/com/example/demo/core/FilterChainExceptionHandlerFilter.java
<filename>demo_app/server/src/main/java/com/example/demo/core/FilterChainExceptionHandlerFilter.java<gh_stars>0package com.example.demo.core;

import java.io.IOException;

import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class FilterChainExceptionHandlerFilter extends OncePerRequestFilter {

  private final HandlerExceptionResolver resolver;

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {
    try {
      filterChain.doFilter(request, response);
    } catch (Exception exception) {
      resolver.resolveException(request, response, null, exception);
    }
  }
}
