Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.

IHttpExceptionHandler

Jussi Saarivirta edited this page Mar 3, 2019 · 2 revisions

IHttpExceptionHandler

The interface of the Exception Handler. Implement this interface if you need to override default exception handling. The default implementation DefaultHttpExceptionHandler contains the basic exception handling code that returns 400, 401, 403 and 500 errors, simply handling the different HttpExtensions Exceptions and for anything else returning a generic 500 error.

The DefaultHttpExceptionHandler methods are virtual so you may override or supplement them as you like.

To override the default handler you should register your implementation in the dependency injection provider, specifically to replace the default implementation. In practise you need to add the following line to your Function App startup code:

builder.Services.Replace(ServiceDescriptor.Singleton<IHttpExceptionHandler, TImplementation>())

Example:

[assembly: WebJobsStartup(typeof(Startup), "MyStartup")]

namespace AzureFunctionsV2.HttpExtensions.Tests.FunctionApp.Startup
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.Replace(
            ServiceDescriptor.Singleton<IHttpExceptionHandler,
            MyOwnHttpExceptionHandler>());            
        }
    }
}

Troubleshooting

You may run into the issue that your startup code never runs, hence your custom implementation failing to register. This is a somewhat known issue in Functions. Perhaps the simplest workaround (described here) is to move the startup class to a separate assembly (ie. create a new class library and put it there) and then add it as a reference to your main Function App project.

Clone this wiki locally