profile
viewpoint

Ask questionsErrorPageFilter ignores REST response in case of Exception

I'm running a Spring Boot web app with Thymeleaf + Spring MVC with a mix of MVC and REST calls (from JS code). In a @ControllerAdvice I have the following exception handling code :

@ExceptionHandler(MyRuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody MyRestResponse handleMyRuntimeException(MyRuntimeException exception) {
    return new MyRestResponse("Some data I want to send back to the client.");
}

Running Spring Boot with Tomcat embedded container I receive a MyRestResponse object in my ajax failure handler as expected but when I deploy in standalone Tomcat the response is ignored as the ErrorPageFilter forward the exception info instead due to the following code in doFilter() :

if (status >= 400) {
    handleErrorStatus(request, response, status, wrapped.getMessage());
    response.flushBuffer();
}

I went around the problem by writing directly the response to the output stream.

@ExceptionHandler(RuntimeException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public void handleRuntimeException(HttpServletRequest request, HttpServletResponse response, RuntimeException exception) {
        try {
            jacksonMessageConverter.write(new MyRestResult(translateMessage(exception)), MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
            response.flushBuffer(); // Flush to commit the response
            } catch (IOException e) {
            e.printStackTrace();
        }
    }

That solution is ok for me but I think that should be possible to return a @ResponseBody in case of an error happening during a REST call. I have also tried to return a ResponseEntity as suggested by @rstoyanchev here but still see the same issue.

spring-projects/spring-boot

Answer questions pariaSadatHosseiny

@pariaSadatHosseiny In a logback.xml or logback-spring.xml file. You could also configure it in application.properties using logging.level.org.springframework.boot.context.web.ErrorPageFilter=off

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

thank you for Your Answer .I have such kind of Error but i am thinking if use this code just i dont see the Exception in the Log but I dont Understand the Reason,isn't it?!

my Error Is here :

2019-07-14 22:40:33 99839 [qtp1599771323-21] ERROR o.s.b.context.web.ErrorPageFilter - Cannot forward to error page for request [/vendor/jQuery/jquery.js] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false org.eclipse.jetty.io.EofException: null at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:284)

useful!

Related questions

Controller annotated with @Timed and active TimedAspect clashes with WebMvcMetricsFilter hot 2
Actuator: NPE in LongTaskTimingHandlerInterceptor hot 2
spring boot Servlet.service() for servlet [dispatcherServlet] in context with path threw exception hot 2
Migrating OAuth2 from Spring Boot 1.5 to 2.0 Broken hot 2
Classloading problems with Spring Boot, JDK11 and Security Manager hot 1
java.lang.NullPointerException: null occurs in HttpExchangeTracer hot 1
Multiple data source projects cannot be started hot 1
Dependency resolution fails with Gradle 5.3.x to 5.6.x hot 1
get error messag Could not fetch the SequenceInformation from the database, hibernate_sequence doesn't exist hot 1
HttpTraceFilter bean missing hot 1
A java.lang.NoClassDefFoundError: ch/qos/logback/classic/spi/ThrowableProxy was thrown when killing my app hot 1
Source file must be provided failure when running spring-boot:repackage from the command-line hot 1
Issue with spring boot v2.1.2 with DocumentBuilderFactory.setFeature(String) hot 1
ConfigurationProperties with constructor binding cannot be mocked hot 1
Improve diagnostics when configuration property scanning and component scanning both create a bean for the same class hot 1
Github User Rank List