Skip to content
Return exceptions over HTTP e.g. as ASP.NET Core Problem Details, and HTTP-specific exception classes that enable ASP.NET to generate exception information
C# PowerShell
Branch: master
Clone or download
petervandenhout Various fixes (#32)
* Fix ValidationErrorException xmldoc #31
* Change DI to configure using IMvcCoreBuilder or IMvcBuilder #30
* Logging exceptions #19
* Add implementation for "application/problem+xml", e.g. for serializing/deserializing #6
Latest commit 9aaa660 Aug 14, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples/Opw.HttpExceptions.AspNetCore.Sample Various fixes (#32) Aug 14, 2019
tests Various fixes (#32) Aug 14, 2019
.editorconfig Update .editorconfig Jun 21, 2019
.gitignore Release 2.0 (#27) Jun 30, 2019 move documentation to separate folder Jun 14, 2019 Update documentation and sample project (#12) #10 Jun 19, 2019
Directory.Build.props HttpExceptions serializable and replace ExceptionDetails with Seriali… Aug 14, 2019
HttpExceptions.sln Various fixes (#32) Aug 14, 2019
LICENSE Initial commit Jun 11, 2019 Various fixes (#32) Aug 14, 2019
azure-pipelines.yml Various fixes (#32) Aug 14, 2019
http-exceptions-logo-256x256.gif Add logo Jul 16, 2019

HttpExceptions PineBlog

Return exceptions over HTTP

Build Status NuGet Badge License: MIT

Middleware and extensions for returning exceptions over HTTP, e.g. as ASP.NET Core Problem Details. Problem Details are a machine-readable format for specifying errors in HTTP API responses based on But you are not limited to returning exception results as Problem Details, but you can create your own mappers for your own custom formats.

Where can I get it?

You can install Opw.HttpExceptions.AspNetCore from the NuGet package manager console:

C> dotnet add package Opw.HttpExceptions.AspNetCore

Getting started

Add the HttpExceptions services and the middleware in the Startup.cs of your application. First add HttpExceptions using the IMvcBuilder of IMvcCoreBuilder.

public void ConfigureServices(IServiceCollection services)
    // or services.AddMvcCore().AddHttpExceptions();

And for ASP.NET Core 3.x.

public void ConfigureServices(IServiceCollection services)

Then you can add the HttpExceptions middleware using the application builder. UseHttpExceptions should be the first middleware component added to the pipeline. That way the UseHttpExceptions Middleware catches any exceptions that occur in later calls. When using HttpExceptions you don't need to use UseExceptionHandler or UseDeveloperExceptionPage.

public void Configure(IApplicationBuilder app)
    app.UseHttpExceptions(); // this is the first middleware component added to the pipeline

Configuring options

You can extend or override the default behavior through the configuration options, HttpExceptionsOptions.

Include exception details

Whether or not to include the full exception details in the response. The default behavior is only to include exception details in a development environment.

mvcBuilder.AddHttpExceptions(options =>
    // This is the same as the default behavior; only include exception details in a development environment.
    options.IncludeExceptionDetails= context => context.RequestServices.GetRequiredService<IHostingEnvironment>().IsDevelopment();

Is exception response

Is the response an exception and should it be handled by the HttpExceptions middleware.

mvcBuilder.AddHttpExceptions(options =>
    // This is a simplified version of the default behavior; only map exceptions for 4xx and 5xx responses.
    options.IsExceptionResponse = context => (context.Response.StatusCode < 400 && context.Response.StatusCode >= 600);

Custom ExceptionMappers

Set the ExceptionMapper collection that will be used during mapping. You can override and/or add ExceptionMappers for specific exception types. The ExceptionMappers are called in order so make sure you add them in the right order.

By default there is one ExceptionMapper configured, that ExceptionMapper catches all exceptions.

mvcBuilder.AddHttpExceptions(options =>
    // Override and or add ExceptionMapper for specific exception types, the default ExceptionMapper catches all exceptions.
    options.ExceptionMapper<BadRequestException, BadRequestExceptionMapper>();
    options.ExceptionMapper<ArgumentException, ExceptionMapper<ArgumentException>>();
    // The last ExceptionMapper should be a catch all, for type Exception.
    options.ExceptionMapper<Exception, MyCustomExceptionMapper>();

InvalidModelStateResponseFactory API behavior

By default the Microsoft.AspNetCore.Mvc.ApiBehaviorOptions.InvalidModelStateResponseFactory and related settings are overridden and the configured ExceptionMappers are used. This can be disabled by setting HttpExceptionsOptions.SuppressInvalidModelStateResponseFactoryOverride.

mvcBuilder.AddHttpExceptions(options =>
    options.SuppressInvalidModelStateResponseFactoryOverride = true;

Sample project using HttpExceptions middleware

See the samples/Opw.HttpExceptions.AspNetCore.Sample project for a sample implementation. This project contains examples on how to use the HttpExceptions middleware.

Please see the code :nerd_face


Build Status NuGet Badge License: MIT

HTTP-specific exception classes that enable ASP.NET to generate exception information. These classes can be used by themselves or as base classes for your own HttpExceptions.

Where can I get it?

You can install Opw.HttpExceptions from the NuGet package manager console:

C> dotnet add package Opw.HttpExceptions



  • 400 BadRequestException
  • 400 InvalidModelException
  • 400 ValidationErrorException<T>
  • 400 InvalidFileException
  • 401 UnauthorizedException
  • 403 ForbiddenException
  • 404 NotFoundException
  • 404 NotFoundException<T>
  • 409 ConflictException
  • 409 ProtectedException
  • 415 UnsupportedMediaTypeException


  • 500 InternalServerErrorException
  • 500 DbErrorException
  • 500 SerializationErrorException
  • 503 ServiceUnavailableException


We accept fixes and features! Here are some resources to help you get started on how to contribute code or new content.

Copyright © 2019, Of Pine Wood. Created by Peter van den Hout. Released under the terms of the MIT license.

You can’t perform that action at this time.