Table of Contents

Class HttpFaultResolver

Namespace
Cuemon.AspNetCore.Diagnostics
Assembly
Cuemon.AspNetCore.dll

Provides a way to evaluate an exception and provide details about it in a developer friendly way, optimized for open- and otherwise public application programming interfaces (API).

public class HttpFaultResolver : FaultHandler<HttpExceptionDescriptor>
Inheritance
HttpFaultResolver
Inherited Members

Examples

The following example demonstrates how to create an HTTP fault resolver that maps exceptions to HTTP error responses.

using System;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.Diagnostics;

namespace MyApp.Examples;

public class HttpFaultResolverExample
{
    public void Demonstrate()
    {
        var resolver = new HttpFaultResolver(
            exception => exception is ArgumentNullException,
            exception => new HttpExceptionDescriptor(exception, 400, null, "Argument was null."));

        if (resolver.TryResolveFault(new ArgumentNullException("value"), out var descriptor))
        {
            Console.WriteLine(descriptor.StatusCode); // 400
        }

        var resolved = resolver.TryResolveFault(new InvalidOperationException(), out _);
        Console.WriteLine(resolved); // False
    }
}

Constructors

HttpFaultResolver(Func<Exception, bool>, Func<Exception, HttpExceptionDescriptor>)

Initializes a new instance of the HttpFaultResolver class.

public HttpFaultResolver(Func<Exception, bool> validator, Func<Exception, HttpExceptionDescriptor> descriptor)

Parameters

validator Func<Exception, bool>

The function delegate that evaluates an Exception.

descriptor Func<Exception, HttpExceptionDescriptor>

The function delegate that provides details about an Exception.

Exceptions

ArgumentNullException

validator cannot be null -or- descriptor cannot be null.