Custom error handling for 5xx errors in Sitecore 9.3

Writing this post since I was trying to find how to handle custom error in Sitecore 9.3 and found too many posts that was confusing since they are trying to modify the Global.asax and trying to handle error that way.

It is not the best practice to modify Global.asax the was put by Sitecore. You should use one of the pipeline hooks.

Sitecore Pipeline (omitted few things for brevity)

  • Initialize
    • InilializeGlobalFilters
      • PipelineBasedRequestFilter
        • OnException (this is what we want to hook on to)

To hook on that OnException, we have to create a patch file

<sitecore>
  <pipelines>
    <mvc.exception>
		<processor type="Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage, Sitecore.Mvc">
			<patch:attribute name="type">[my namespace].[my method], [my namespace]</patch:attribute>
		</processor>
	</mvc.exception>
	<pipelines>
</sitecore>

Also we will need to change the web.config file to edit the customerror mode.

<customeErrors mode= "RemoteOnly | On" />

I like to put the page right on my root. So I will create a page like error.html and have some type of message saying error happened or something like that.

Now, every time you have a 500 error, your custom error.html will show.