16

I am using spring MVC framework. I want to log error statuses whenever exception is thrown, so afterCompletion method is used in HanlderInterceptor.

@Override
public void afterCompletion( final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex)
{
    final int responseCode = response.getStatus();
    s_logger_error.error("status code: " + responseCode );
}

This code works fine if I run this as an application on local machine. But when we host it on the jetty server, UI gets correct error response (In my case 409), but in this method it gets logged as 200.

[Image from remote debug where it shows status=200 but in response it is 409] enter image description here

Can somebody help to figure out why there is change in the response code?

I am using sprint 1.1.7.RELEASE spring boot version and jetty-distribution-9.2.10.v20150310.

Bendy
  • 3,506
  • 6
  • 40
  • 71
subhashlg26
  • 993
  • 1
  • 11
  • 25

1 Answers1

1

You need to make sure that you call setStatus method on your response object on exception.

If you assert this, than upgrading to spring boot version 1.1.11 can fix your issue. It concerns this fix. Prior to the fix, the ErrorPageFilter was masking the response status of wrapped response in the cases where sendError method is not explicitely called on the ErrorWrapperResponse.

The code after the fixed changed from simple return this.status to

        if (this.errorToSend) {
            return this.status;
        }
        else {
            // If there was no error we need to trust the wrapped response
            return super.getStatus();
        }

that is why I believe that the upgrade will fix your issue.

Finally, if you issue persist, debugging through ErrorPageFilter should indicate the origin of the problem

Master Slave
  • 27,771
  • 4
  • 57
  • 55