Disabling IIS 7.5 server error response for 5xx and 4xx HTTP status codes
Solution 1:
This was answered in another question: IIS7 Hijacks My Coldfusion Error Page
Seems that the solution is changing the configuration so that IIS ignores the error and allows it to pass through, hence the setting
<httpErrors existingResponse="PassThrough" />
So the solution is to change the behavior of IIS, which is fortunate, because requiring every existing web application server to change their behavior would be really stupid.
Update
Please see my updated answer in another similar question to find out why and how there is no workable answer to this problem.
Solution 2:
There is apparently a way to do this using Response.TrySkipIisCustomErrors. The below example is from an aspx page but should be adaptable to your use case.
This following web content is too extensive to duplicate here.
http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors