getsentry / sentry-docs

Sentry's documentation (and tools to build it)
https://docs.sentry.io
Other
332 stars 1.46k forks source link

ASP.net setup process does not work when customerrors is turned on with HandleErrorAttribute (very common setup) #6724

Open jasekiw opened 1 year ago

jasekiw commented 1 year ago

Core or SDK?

Platform/SDK

Which part? Which one?

ASP.NET

Description

Documentation can be found here: https://docs.sentry.io/platforms/dotnet/guides/aspnet/

This page does not mention that this setup stops working when custom errors is turned on in the web.config. example:

<customErrors mode="On" xdt:Transform="Replace">
  <error statusCode="404" redirect="~/Error/Error404" />
  <error statusCode="401" redirect="~/Error/Unauthorized" />
  <error statusCode="403" redirect="~/Error/Forbidden" />
  <error statusCode="500" redirect="~/Error/Error500" />
</customErrors>

This is because Application_Error in Galobal.asax.cs does not get called when customErrors is turned on.

Here is a stack overflow post describing the issue here: https://stackoverflow.com/questions/6508415/application-error-not-firing-when-customerrors-on/9572858#9572858

This is a very common way to setup error handling in

Suggested Solution

My solution was to turn off customErrors and implement it at a lower level as described by this article that the stack overflow post links: https://kitsula.com/Article/MVC-Custom-Error-Pages

This way the Application_Error method is stilled called and the error pages are still presented via razor pages without doing a redirect.

My end configuration looked like this:

<httpErrors errorMode="Custom" existingResponse="Replace" xdt:Transform="Insert">
  <remove statusCode="401" subStatusCode="-1" />
  <remove statusCode="403" subStatusCode="-1" />
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="401" path="/Error/Unauthorized" responseMode="ExecuteURL" />
  <error statusCode="403" path="/Error/Forbidden" responseMode="ExecuteURL" />
  <error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL" />
  <error statusCode="500" path="/Error/Error500" responseMode="ExecuteURL" />
</httpErrors>

A small writeup in this documentation section should be written to describe the problem and the possible solutions that the user would want to use. I think the solution I used is good while preventing 302 redirects.

getsantry[bot] commented 1 year ago

Assigning to @getsentry/support for routing, due by (sfo). ⏲️

getsantry[bot] commented 1 year ago

Routing to @getsentry/owners-native for triage, due by (vie). ⏲️

getsantry[bot] commented 1 year ago

Routing to @getsentry/team-web-sdk-backend for triage, due by (sfo). ⏲️

getsantry[bot] commented 1 year ago

Routing to @getsentry/team-mobile for triage, due by (sfo). ⏲️

mattjohnsonpint commented 1 year ago

Thanks for letting us know about this. I'll investigate and see what the best path forward is here.

github-actions[bot] commented 1 year ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

jasekiw commented 1 year ago

@mattjohnsonpint Can you make sure this issue gets a status so that github actions doesn't automatically close it?