Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing Exception with inner SecurityException only displays inner exception in ASP.NET MVC

If I add the following line to an ASP.NET MVC action method

throw new Exception("outer", new SecurityException("inner"));

the error that is actually displayed on the yellow screen of death is the inner SecurityException with absolutely no mention of the outer exception.

SecurityException

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: inner

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SecurityException: inner]

Is this expected behavior?

It doesn't seem to matter what type the outer exception is. Even if it is another SecurityException, the message is never displayed. The default SecurityException error message is so vague that I want to catch it and add some more specific information. This works fine if I do not include the original SecurityException as the innerException but ideally I would like to do this.

like image 534
Paul Hiles Avatar asked Feb 16 '12 11:02

Paul Hiles


People also ask

How do you throw an inner exception?

By using throw keyword in the catch block, we can re-throw an exception that is handled in the catch block. The re-throwing an exception is useful when we want to pass an exception to the caller to handle it in a way they want.

How can I see inner exceptions?

When you're debugging and you get an exception, always, always, always click on the View Details link to open the View Details dialog. If the message in the dialog isn't expanded, expand it, then scan down to the Inner Exception entry.

What is see the inner exception for details?

An object that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor.

Does exception Tostring include inner exception?

Yes. It prints out the inner exception information as well. If you need more elaborate formatting, you can always loop through the extensions and build a complex string yourself (preferably using a StringBuilder).


1 Answers

This behaviour originates in the ASP.NET "core", not in ASP.NET MVC. Unfortunately, the error formatter classes are internal, and the consuming types do not provide any extension points that would allow one to tweak the behaviour without replacing the error reporting mechanism. The workaround is to replace the default "yellow screen of death" page by a custom error page/view in which one exposes the information that one prefers.

This is exactly what one should usually be doing for production. In your case, it just means that you would have an alternate version for debug instead of using the ASP.NET-provided default.

like image 124
Nicole Calinoiu Avatar answered Nov 15 '22 06:11

Nicole Calinoiu