Thursday, August 23, 2018

Custom error page not displaying on the CD server but works on local


basically that application error function of global.asax is reached only when there is an unhandled exception and hence i had to throw it from the pipeline.

To debug Debu had to add this to a controller for force a 500 error:

Response.Clear();
            Response.StatusCode = 500;
            Response.End();


Then he made the following changes:


using System;
using System.Net;
using System.Web;
using Sitecore.Configuration;
using Sitecore.Pipelines.HttpRequest;

namespace IsacaDP.Foundation.SitecoreExtensions.Pipelines.ErrorHandling
{
public class Set404StatusCode : HttpRequestBase
{
protected override void Execute(HttpRequestArgs args)
{
var context = HttpContext.Current;
// check for 500 error
if (HttpContext.Current.Response.StatusCode == 500)
{
//Unless you thorw an exception, the Application error handler in Global.asax is not called
throw new Exception("500 server error");
}

// retain 500 response if previously set
if (HttpContext.Current.Response.StatusCode >= 500 || context.Request.RawUrl == "/")
{
return;
}

// return if request does not end with value set in ItemNotFoundUrl, i.e. successful page
if (!context.Request.Url.LocalPath.EndsWith(Settings.ItemNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
{
return;
}

_404Logger.Log.Warn("Page Not Found: " + context.Request.RawUrl + ", current status: " + HttpContext.Current.Response.StatusCode);
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound;
HttpContext.Current.Response.StatusDescription = "Page not found";
}
}
}


Then in Global.asax

protected void Application_Error(object sender, EventArgs e)
{
var customErrorsSection = (System.Web.Configuration.CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors");

//display the custom error page regardless of what is the setting in config file
//if (customErrorsSection.Mode != System.Web.Configuration.CustomErrorsMode.Off)
{
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
//You won't be able to override the values if already set by system
if (HttpContext.Current.Response.StatusCode == 0)
{
HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
HttpContext.Current.Response.StatusDescription = "500 Internal Server Error";
}

var errorPageUrl = GetGeneralErrorPage();
HttpContext.Current.Server.TransferRequest(errorPageUrl);
}
}

And on the 500 sitecore item he changed the datasource for the Error rendering to point not to itself but to a valid ds:

ID: "f65e163b-fcf9-48cf-a054-a678d2a31aa4"
Parent: "b02617ad-3ac2-4822-adeb-ed2665a86ed2"
Template: "1af5a037-d949-460a-9ee7-d6e49f981858"
Path: /sitecore/content/ISACA DP/CLP/Home/error pages/500
DB: master
SharedFields:
- ID: "f1a1fe9e-a60c-4ddb-a3a0-bb5b29fe732e"
Hint: __Renderings
Type: layout
Value: |
<r xmlns:p="p" xmlns:s="s"
p:p="1">
<d
id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"
s:l="{7CEB5C70-8560-4004-8C5B-34DCC45C52A0}">
<r
uid="{40E0CE45-96D5-4A0B-983D-8B705841CB27}"
s:ds="{E6CA9BB6-EE74-4812-88F2-4C0172357551}" />
<r
uid="{53B64B34-B76D-47D2-B84C-D16410B113E7}"
s:ds="{FA93ED54-3BD9-42FC-B096-FFE73BBA6470}"
s:par="" />
<r
uid="{1390F179-6445-46F9-8576-B0E5855A48E8}"
s:ds="{FA93ED54-3BD9-42FC-B096-FFE73BBA6470}"
s:par="" />
<r
uid="{44693FF3-D233-4254-A04C-509A26ECBC2F}"
s:ds="{7D7C131C-4B27-4405-B9A0-2C3AA0AFBA61}" />
</d>
</r>
Languages:
- Language: en
Versions:
- Version: 1
Fields:
- ID: "0e065aea-4633-42a7-bb4f-be985b6e971d"
Hint: Description
Value:
- ID: "25bed78c-4957-4165-998a-ca1b52f67497"
Hint: __Created
Value: 20180419T194538Z
- ID: "815cd98b-e279-403e-8687-f9a577d8b528"
Hint: Title
Value: System Error

No comments:

Post a Comment