15

In ASP.NET when you are in DEBUG mode and something fails you get a the famous yellow screen of death.

It shows that there was a Server Error in <Location> Application, provides a description, exception details, source file and stack trace.

I would like to extend this error page to include some extra information.

  • Is there an "open source" implementation of the yellow screen of death?
  • Is there a way to extend the built-in yellow screen of death?

4 Answers 4

10

The error page formatting is done by System.Web.ErrorFormatter and its subclasses, which unfortunately are internal. You can grab the HTML and modify it if you like using HttpException.GetHtmlErrorMessage(). But I'd be inclined to do as other commenters have suggested and use Server.GetLastError and format it myself.

If so - please ensure you HtmlEncode the error message output to mitigate XSS and ensure your production sites don't display any information about the error (as this was the attack vector for the ASP.NET padding oracle decryption attack in 2010).

5
  • the trouble with the proposed solution with HttpException is that there seems to be no way to yank the HttpException, you can only intercept the SqlException ... Commented Sep 5, 2011 at 9:19
  • How about simply new-ing up an HttpException passing your SqlException as an InnerException and then calling GetHtmlErrorMessage(). Commented Sep 5, 2011 at 9:29
  • tried that ... did not get the formatter set ... only options seems to be DIY or an http module Commented Sep 5, 2011 at 10:21
  • 1
    Looks like (according to Reflector) if you newed up the subclass HttpUnhandledException using HttpUnhandledException(string message, Exception innerException) it calls SetFormatter... Commented Sep 5, 2011 at 13:36
  • true ... see samsaffron.com/archive/2011/09/08/… ... Im accepting this Commented Sep 8, 2011 at 5:16
4

This page seems to have the info you're looking for.

http://www.asp.net/hosting/tutorials/processing-unhandled-exceptions-cs

Essentially, when an exception bubbles up to to the runtime it gets wrapped in an System.Web.HttpException (This was wrong. At least I didn't see this behavior in MVC 3).

I'd think you can extend that class and override GetHtmlErrorMessage to return whatever extra data you want.

Update: Well, with a little experimentation it appears I'd be wrong. BUT there is a workaround...

You can write the info from the exception in any manner you choose then clear the error and you'll have what you want returned.

Something like this...

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var error = context.Server.GetLastError();
    context.Response.Write(error.Message);
    context.Response.Write("<br /><br />");
    context.Response.Write(error.StackTrace);
    context.Response.Write("<br /><br />");
    context.Response.Write("Hello World");
    context.Server.ClearError();
}
6
  • In theory this works, however in mvc3 when it hits Application_Error you have a SqlException ... not an HttpException Commented Sep 5, 2011 at 6:16
  • re your edit, yeah ... I got that far ... its just that I don't have and HttpException so can not render the html for the yellow screen unless I hand code it Commented Sep 5, 2011 at 6:17
  • View Source and extract'll save you the trouble. Then set up a nice razor template that'll render the exception and whatever extra data you want from it? Commented Sep 5, 2011 at 6:22
  • yeah ... I guess ... its a PITA ... thought there was an easier way Commented Sep 5, 2011 at 6:24
  • @Sam: Yeah, it seems MS thought better of extensibility at a bunch of places in the framework... Commented Sep 5, 2011 at 6:26
1

If you are using custom error pages, I believe you can get the last error with HttpContext.Current.Server.GetLastError(); I'm not sure if there are any custom pages out there. But the content of the YSOD should be available from the context of the request.

0

Yes there is.

See here:
http://www.oschina.net/code/explore/mono-2.8.1/mcs/class/System.Web/System.Web/HttpException.cs

It's from the source code of mono (.NET for Linux), in versions < 2.10 (that is to say 2.8). In version 2.10+, they started using templates, which makes it far more difficult to find and understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.