HttpExceptions has been updated to version 3 (3.0.2). This new release adds the following new features:

  • New options to override the Exception*Mapping and HttpContext*Mapping methods on the mappers
  • Expose serialization helper methods in the Opw.HttpExceptions.AspNetCore package
  • Opw.HttpExceptions.AspNetCore package is now targeting net50 and .NETCoreApp 3.1

Override the *Mapping methods

New options to override the Exception*Mapping and HttpContext*Mapping methods on the mappers using a Func have been added.

You can inject your own mappings for the ProblemDetails properties using functions on the HttpExceptionsOptions, or by creating your own IExceptionMapper and/or IHttpResponseMapper. If you inject your own function that will be tried first, and if no result is returned the defaults will be used.

In the following example we will create a custom function mapping the ProblemDetails.Type property. By default the ProblemDetails.Type property will be set by:

  1. Either the Exception.HelpLink or the HTTP status code information link.
  2. Or the DefaultHelpLink will be used.
  3. Or an URI with the HTTP status name ("error:[status:slug]") will be used.

When the ExceptionTypeMapping or HttpContextTypeMapping are set the result of those functions will be tried first, if no result is returned the defaults will be used.

mvcBuilder.AddHttpExceptions(options =>
{
    ExceptionTypeMapping = exception => {
        // This is a example, you can implement your own logic here.
        return "My Exception Type Mapping";
    },
    HttpContextTypeMapping = context => {
        // This is a example, you can implement your own logic here.
        return "My HttpContext Type Mapping";
    }
});

Serialization helpers

Serialization helper methods added to the Opw.HttpExceptions.AspNetCore package.

Serialize the HTTP content to ProblemDetails.

ProblemDetails problemDetails = ((HttpContent)response.Content).ReadAsProblemDetails();

Try to get the exception details from the ProblemDetails.

SerializableException exception;
((ProblemDetails)problemDetails).TryGetExceptionDetails(out exception);

Try to get the errors dictionary from the ProblemDetails.

var IDictionary<string, object[]> errors;
((ProblemDetails)problemDetails).TryGetErrors(out errors);

.Net 5.0

The Opw.HttpExceptions.AspNetCore package is now targeting net50 and .NETCoreApp 3.1.

Where can I get it?

The code can be found on GitHub at: github.com/ofpinewood/http-exceptions. And there is also a sample project you can have a look at.

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

PM> Install-Package Opw.HttpExceptions
PM> Install-Package Opw.HttpExceptions.AspNetCore