Class HttpExceptionDescriptorResponseFormatter<TOptions>
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assembly
- Cuemon.AspNetCore.dll
Provides a generic way to support content negotiation for exceptions in the application.
public class HttpExceptionDescriptorResponseFormatter<TOptions> : Configurable<TOptions>, IConfigurable<TOptions>, IHttpExceptionDescriptorResponseFormatter where TOptions : class, IContentNegotiation, IParameterObject, new()
Type Parameters
TOptionsThe type of the options to configure.
- Inheritance
-
Configurable<TOptions>HttpExceptionDescriptorResponseFormatter<TOptions>
- Implements
-
IConfigurable<TOptions>
- Inherited Members
Examples
The following example demonstrates how to use to support content negotiation for exceptions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon.Configuration;
using Cuemon.Net.Http;
namespace Cuemon.AspNetCore.Diagnostics;
public static class HttpExceptionDescriptorResponseFormatterExample
{
public static void Demonstrate()
{
var formatter = new HttpExceptionDescriptorResponseFormatter<SampleFormatterOptions>(_ => { });
formatter.Populate((exceptionDescriptor, mediaType) =>
new StringContent($"{exceptionDescriptor.StatusCode}:{mediaType.MediaType}"));
var exceptionDescriptor = new HttpExceptionDescriptor(
new InvalidOperationException("boom"),
418,
"Teapot",
"Short and stout");
using var response = formatter.ExceptionDescriptorHandlers.First().ToHttpResponseMessage(exceptionDescriptor);
Console.WriteLine(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
}
private sealed class SampleFormatterOptions : IContentNegotiation, IParameterObject
{
public IReadOnlyCollection<MediaTypeHeaderValue> SupportedMediaTypes { get; } =
new[] { new MediaTypeHeaderValue("text/plain") };
}
}
Constructors
HttpExceptionDescriptorResponseFormatter(IOptions<TOptions>)
Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class.
public HttpExceptionDescriptorResponseFormatter(IOptions<TOptions> options)
Parameters
optionsIOptions<TOptions>The IOptions<TOptions> which need to be configured.
Exceptions
- ArgumentNullException
optionscannot be null.- ArgumentException
optionsare not in a valid state.
HttpExceptionDescriptorResponseFormatter(Action<TOptions>)
Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class.
public HttpExceptionDescriptorResponseFormatter(Action<TOptions> setup)
Parameters
Exceptions
- ArgumentException
setupcould not be configured to a valid state.
HttpExceptionDescriptorResponseFormatter(TOptions)
Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class.
public HttpExceptionDescriptorResponseFormatter(TOptions options)
Parameters
optionsTOptionsThe configured options of this instance.
Exceptions
- ArgumentNullException
optionscannot be null.- ArgumentException
optionsare not in a valid state.
Properties
ExceptionDescriptorHandlers
Gets the collection of populated HttpExceptionDescriptorResponseHandler instances.
public ICollection<HttpExceptionDescriptorResponseHandler> ExceptionDescriptorHandlers { get; }
Property Value
- ICollection<HttpExceptionDescriptorResponseHandler>
The collection of populated HttpExceptionDescriptorResponseHandler instances.
Methods
Adjust(Action<TOptions>)
Adjusts the Options to your liking.
public HttpExceptionDescriptorResponseFormatter<TOptions> Adjust(Action<TOptions> setup)
Parameters
Returns
- HttpExceptionDescriptorResponseFormatter<TOptions>
A reference to this instance so that additional calls can be chained.
Exceptions
- ArgumentException
setupcould not be configured to a valid state.
Populate(Func<HttpExceptionDescriptor, MediaTypeHeaderValue, HttpContent>, ICollection<HttpExceptionDescriptorResponseHandler>)
Populates the underlying SupportedMediaTypes to the ExceptionDescriptorHandlers using the specified contentFactory.
public HttpExceptionDescriptorResponseFormatter<TOptions> Populate(Func<HttpExceptionDescriptor, MediaTypeHeaderValue, HttpContent> contentFactory, ICollection<HttpExceptionDescriptorResponseHandler> exceptionDescriptorHandlers = null)
Parameters
contentFactoryFunc<HttpExceptionDescriptor, MediaTypeHeaderValue, HttpContent>The function delegate that will create an instance of HttpContent with input from the specified HttpExceptionDescriptor and MediaTypeHeaderValue.
exceptionDescriptorHandlersICollection<HttpExceptionDescriptorResponseHandler>The optional collection of HttpExceptionDescriptorResponseHandler instances to populate to.
Returns
- HttpExceptionDescriptorResponseFormatter<TOptions>
A reference to this instance so that additional calls can be chained.
Exceptions
- ArgumentNullException
contentFactorycannot be null.