Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customErrors vs Custom modules

I've currently got httpErrors setup to deal with 500's here:-

<httpErrors errorMode="Custom" existingResponse="Replace">
        ......
        <remove statusCode="500"/>
        <error statusCode="500" path="/server-error" responseMode="ExecuteURL"/>
</httpErrors>

This works fine, but in the case where iis receives the error I still get the yellow screen of death. An example is when entity framework can not connect to the database and I receive:-

Cannot open database "Test-DB" requested by the login.
The login failed.
Login failed for user 'sa'.

I've setup customErrors to deal with this:-

<customErrors mode="On" defaultRedirect="error.html" redirectMode="ResponseRedirect">
        <error statusCode="500" redirect="error.html" />
</customErrors>

which works as expected as long as there is no modules without preCondition="managedHandler".

I have a few modules which deal with images and css files and are in the same project.

<modules runAllManagedModulesForAllRequests="false">
        <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
        <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
        <add name="ImageHandler" type="foo.bar.ProductImageHandlerHttpModule" />
        <add name="CustomCssHandler" type="foo.bar.CustomCssHttpModule" />
        <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" />
</modules>

Comment these out and I get the error.html, keep them in and I get

Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

showing that a module from the project is also erroring when trying to show the error.html.

Does anyone know a fix/workaround?

like image 947
BenG Avatar asked Oct 20 '16 10:10

BenG


People also ask

In which file the customErrors is specified?

config settings, customErrors can be configured within the Machine. config, root web. config or your application's web. config file.

What is custom error mode off?

If you set custom errors to Off , a detailed status page is displayed to all users. If you set custom errors to RemoteOnly, a detailed page is displayed to users that are requesting the page from the host server and a general status page is displayed to all users that request the site from other machines.

Which of the following attributes of the customErrors section is used to show custom error page in asp net?

The <customErrors> section in Web. config has two attributes that affect what error page is shown: defaultRedirect and mode . The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD.


1 Answers

Its a tough situation - your error comes from HttpModule, which is run for every single request - including request to error.html page. One way is to route static files (such as error.html) just via your server - and ignore them on .NET level; this may not be always possible (sometimes handling static files on .NET level is handy). The only other way I can think off is hook in global.asax to error event and handle it yourself (which will ignore the customErrors)

Something like:

public class Global : System.Web.HttpApplication
{
    protected void Application_Error(object sender, EventArgs e)
    {
        // is there an error ?
        var error = Server.GetLastError();
        if (error != null)
        {
            // mark the error as - "i'll handle it myself"
            Server.ClearError();
            // load error.html manually & dump it to response
            var content = File.ReadAllText(Server.MapPath("~/error.html"));
            Context.Response.Write(content);
            // set correct error code
            Context.Response.StatusCode = 500;
        }
    }
}

Note: this can be ironed out, but you see the general principle ...

like image 96
Ondrej Svejdar Avatar answered Oct 07 '22 10:10

Ondrej Svejdar