Class FaultDescriptorOptionsDecoratorExtensions
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assembly
- Cuemon.AspNetCore.dll
Extension methods for FaultDescriptorOptions class hidden behind the IDecorator<T> interface.
public static class FaultDescriptorOptionsDecoratorExtensions
- Inheritance
-
FaultDescriptorOptionsDecoratorExtensions
Examples
FaultDescriptorOptionsDecoratorExtensions provides extension methods on Decorator.Enclose for resolving HTTP exception descriptors from failure objects using FaultDescriptorOptions. This example creates a FaultDescriptorOptions instance and a BadRequestException as the failure input, then wraps the options with Decorator.Enclose and calls TryResolveHttpExceptionDescriptor with the failure and an HttpContext. The resolved descriptor's StatusCode is output as 400, confirming the failure was correctly mapped to a Bad Request HTTP response.
using System;
using Cuemon;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
namespace MyApp.Examples;
public class FaultDescriptorOptionsDecoratorExtensionsExample
{
public void Demonstrate(HttpContext context)
{
var options = new FaultDescriptorOptions();
var failure = new BadRequestException("Bad request");
Decorator.Enclose(options).TryResolveHttpExceptionDescriptor(
failure,
context,
descriptor => Console.WriteLine("Resolved descriptor"),
out var descriptor);
Console.WriteLine(descriptor.StatusCode); // 400
}
}
Methods
TryResolveHttpExceptionDescriptor<T>(IDecorator<T>, Exception, HttpContext, Action<HttpExceptionDescriptor>, out HttpExceptionDescriptor)
Tries to resolve an HttpExceptionDescriptor from the specified failure and context.
public static bool TryResolveHttpExceptionDescriptor<T>(this IDecorator<T> decorator, Exception failure, HttpContext context, Action<HttpExceptionDescriptor> onBeforeExceptionFactory, out HttpExceptionDescriptor descriptor) where T : FaultDescriptorOptions
Parameters
decoratorIDecorator<T>The decorator that encapsulates the FaultDescriptorOptions.
failureExceptionThe Exception that represents the failure.
contextHttpContextThe HttpContext in which the failure occurred.
onBeforeExceptionFactoryAction<HttpExceptionDescriptor>A delegate to invoke just before the ExceptionCallback is called.
descriptorHttpExceptionDescriptorWhen this method returns, contains the resolved HttpExceptionDescriptor, if the resolution succeeded, or
nullif the resolution failed.
Returns
- bool
trueif the HttpExceptionDescriptor was resolved successfully; otherwise,false.
Type Parameters
TThe type of the FaultDescriptorOptions.
Exceptions
- ArgumentNullException
decoratorcannot be null -or-failurecannot be null -or-contextcannot be null.