Skip to content

Conversation

@dhaval24
Copy link
Contributor

@dhaval24 dhaval24 commented Mar 11, 2018

This PR fixes the issue where the where the WebRequestTelemetryFilter stamps the response code as 200 OK when the Servlet Throws.

The fix includes explicitly setting the response code as 500 on the RequestTelemetry item when filter catches RunTimeException, IOExceptionor ServletException. This needs to be set explicitly because the response code officially get's set from org.apache.catalina.core. StandardWrapparValue class's invoke() method in it's catch block. This method is called way later after the FilterChain completes it's execution.

This PR also Deprecates the ApplicationInsightsHttpResponseWrapper class and all the places where casting was done to this, as we no longer require this class as of Servlet API 3.0.1 onwards. It already has getStatus() method for which this wrapper was designed.

Fix #600

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated

@dhaval24 dhaval24 requested review from grlima and littleaj March 11, 2018 02:30
@dhaval24 dhaval24 self-assigned this Mar 11, 2018
@dhaval24 dhaval24 added the Bug label Mar 11, 2018
@dhaval24 dhaval24 added this to the 2.0.2 milestone Mar 11, 2018
@dhaval24
Copy link
Contributor Author

All broken tests are fixed. It needed Servlet API as testCompile also.

Copy link
Contributor

@littleaj littleaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to write some integration tests for this and commit them to this branch.

//later in the the chain inside the catch block of org.apache.catalina.core.StandardWrapperValue class
//The call to this method happens after the Filter Chain has been completed and thus response object
//can never have this value at this time.
response.setStatus(500);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this. We must discover what the app is returning to the client.

Copy link
Contributor Author

@dhaval24 dhaval24 Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@littleaj if you have better alternatives I am happy to take that. But in all my test I did not found any way to control this behavior. Filter chain gets released way before 500 is set.

AFAIK it's easy to verify this by checking the source code of the StandardWrapperValue these three exceptions will always lead to "500". Every other response code is set before the filter sets the value so I do not think that we would be doing any harm here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of non-tomcat app servers where some other class will govern setting this value, it would also follow the same patter of setting 500. Now ordering might change but the value of response code should be the same in any app server regardless for this three exceptions.

@dhaval24
Copy link
Contributor Author

Please take a look at the last comment here : https://stackoverflow.com/questions/20772837/java-servlet-throw-http-500-error-on-purpose

@dhaval24
Copy link
Contributor Author

@littleaj I got your explanation now. As a work around as discussed we can set the resultcode to un known and the success = false. Ideally as suggested we might need to hook at lower level then filters.

We can rest of the goodies from the branch like deprecating ApplicationInsightsHttpResponseWrapper

@dhaval24
Copy link
Contributor Author

Closing as we are researching on better alternatives.

@dhaval24 dhaval24 closed this Apr 26, 2018
@DineshAyyapillai
Copy link

@littleaj @dhaval24 Do we find any alternative solution for this issue? we are also looking for fix similar to this issue

@trask trask deleted the fixResponseCodeWhenServletThrows branch September 21, 2020 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter doesn't see request result code when Servlet throws exceptions

4 participants