Search code examples
c#asp.net-mvcerror-handlingyellow-screen-of-death

How would I hook into/replace the "end user" YSOD?


I want to display the YSOD when it would be providing useful information during development or locally on the servers but a semi-generic page in other cases. I know that I could set the defaultRedirect attribute of the application's <customErrors> configuration tag in web.config, but I would rather do some processing to generate a page with slightly better information.

All of my controllers inherit from one central BaseController class where I have overridden OnException (essentially like this):

protected override void OnException(ExceptionContext filterContext) {
    //if something really bad happened and we get inside this if, 
    //just let the YSOD appear because there isn't anything we can do
    if (filterContext == null)
        return; 

    LogException(filterContext.Exception);

    //insert answer for question here:
    if (FigureOutIfDetailedYsodWouldBeDisplayed(filterContext)) 
        return;

    //what to actually do for end users
    filterContext.ExceptionHandled = true;
    filterContext.Result = View("ErrorPage", GetErrorModel(filterContext));
}

How should I implement FigureOutIfDetailedYsodWouldBeDisplayed (answer need not be code, a pointer in the right direction would be just fine)? My current implementation checks the raw url for the existence of "//localhost", but this solution feels clumsy and doesn't work all the time (for example if the dev has a host entry to type something other than localhost: a requirement our app used to have).


Solution

  • If you actually build using the Debug setting for Dev environments, and Release for production environments, you could always just do

    #if DEBUG
        //Show YSOD
    #else
        //Show friendly error page
    #endif