Table of Contents

HttpStatusCodeExceptionDecoratorExtensions Class

Definition

Extension methods for the HttpStatusCodeException class hidden behind the IDecorator<T> interface.

public static class HttpStatusCodeExceptionDecoratorExtensions
Inheritance
HttpStatusCodeExceptionDecoratorExtensions

Examples

The following example demonstrates how to add response headers to an HttpStatusCodeException using the decorator pattern.

// Program.cs
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System.Linq;
using Cuemon;

var exception = new NotFoundException("Resource not found.");
var headers = new HeaderDictionary
{
    { "X-Correlation-Id", new StringValues("abc-123") },
    { "X-Request-Id", new StringValues("req-456") }
};

Decorator.Enclose(exception).AddResponseHeaders(headers);

using var message = new HttpResponseMessage();
message.Headers.Add("X-Server-Id", "server-01");

Decorator.Enclose(exception).AddResponseHeaders(message.Headers);

Console.WriteLine($"Correlation header: {exception.Headers["X-Correlation-Id"]}");
Console.WriteLine($"Request header: {exception.Headers["X-Request-Id"]}");
Console.WriteLine($"Server header: {exception.Headers["X-Server-Id"]}");

Methods

Name Description
AddResponseHeaders<T>(IDecorator<T>, IHeaderDictionary)

Adds all non-existing headers to the enclosed HttpStatusCodeException of the decorator.

AddResponseHeaders<T>(IDecorator<T>, HttpResponseHeaders)

Adds all non-existing headers to the enclosed HttpStatusCodeException of the decorator.

See Also