Skip to main content

arolariu.Backend.Common.Http

arolariu.Backend.Common

arolariu.Backend.Common.Http Namespace

Classes

ExceptionMappingHandler Class

Global Microsoft.AspNetCore.Diagnostics.IExceptionHandler that routes unhandled exceptions through ExceptionToHttpResultMapper, ensuring a consistent RFC 7807 ProblemDetails response even when an exception escapes an endpoint's local try/catch.

public sealed class ExceptionMappingHandler : Microsoft.AspNetCore.Diagnostics.IExceptionHandler

Inheritance System.Object 🡒 ExceptionMappingHandler

Implements Microsoft.AspNetCore.Diagnostics.IExceptionHandler

Remarks

Endpoints still wrap their handler bodies in try/catch for fast-path telemetry; this handler is belt-and-suspenders for middleware faults and pre-handler throws (model binding, routing, authentication middleware, etc.). On any caught exception it records the exception on the current System.Diagnostics.Activity, maps it to a ProblemDetails result, executes the result, and returns true to mark the exception as handled.

Methods

ExceptionMappingHandler.TryHandleAsync(HttpContext, Exception, CancellationToken) Method

Translates exception into an RFC 7807 ProblemDetails response via ToHttpResult(Exception, Activity) and writes it to httpContext.

public System.Threading.Tasks.ValueTask<bool> TryHandleAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, System.Exception exception, System.Threading.CancellationToken cancellationToken);

Parameters

httpContext Microsoft.AspNetCore.Http.HttpContext

The current request context; must not be null.

exception System.Exception

The caught exception; must not be null.

cancellationToken System.Threading.CancellationToken

Propagated cancellation token.

Implements TryHandleAsync(HttpContext, Exception, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<System.Boolean>
true once the exception has been mapped and written; or false if the response has already started writing and cannot be safely replaced with a ProblemDetails payload (the framework's fallback handler will observe the caller-provided partial response).

ExceptionToHttpResultMapper Class

Centralized translator from exception markers to RFC 7807 ProblemDetails with proper HTTP status codes. Static because the mapping is a pure function of the exception chain and the current Activity.

public static class ExceptionToHttpResultMapper

Inheritance System.Object 🡒 ExceptionToHttpResultMapper

Methods

ExceptionToHttpResultMapper.ToHttpResult(Exception, Activity) Method

Maps an exception to an Microsoft.AspNetCore.Http.IResult carrying an RFC 7807 ProblemDetails payload.

public static Microsoft.AspNetCore.Http.IResult ToHttpResult(System.Exception exception, System.Diagnostics.Activity? activity);

Parameters

exception System.Exception

activity System.Diagnostics.Activity

Returns

Microsoft.AspNetCore.Http.IResult

ProblemTypeUris Class

Stable RFC 7807 type URIs emitted by ExceptionToHttpResultMapper. Treat these as part of the public API contract — do not rename without bumping the major version of the API.

public static class ProblemTypeUris

Inheritance System.Object 🡒 ProblemTypeUris

Fields

ProblemTypeUris.Conflict Field

Problem type URI for resource conflicts (HTTP 409).

public const string Conflict = "https://api.arolariu.ro/problems/conflict";

Field Value

System.String

ProblemTypeUris.Forbidden Field

Problem type URI for forbidden requests (HTTP 403).

public const string Forbidden = "https://api.arolariu.ro/problems/forbidden";

Field Value

System.String

ProblemTypeUris.InternalServerError Field

Problem type URI for unhandled internal errors (HTTP 500).

public const string InternalServerError = "https://api.arolariu.ro/problems/internal-error";

Field Value

System.String

ProblemTypeUris.Locked Field

Problem type URI for locked resources (HTTP 423).

public const string Locked = "https://api.arolariu.ro/problems/locked";

Field Value

System.String

ProblemTypeUris.NotFound Field

Problem type URI for missing resources (HTTP 404).

public const string NotFound = "https://api.arolariu.ro/problems/not-found";

Field Value

System.String

ProblemTypeUris.RateLimited Field

Problem type URI for rate-limited requests (HTTP 429).

public const string RateLimited = "https://api.arolariu.ro/problems/rate-limited";

Field Value

System.String

ProblemTypeUris.ServiceUnavailable Field

Problem type URI for unavailable services (HTTP 503).

public const string ServiceUnavailable = "https://api.arolariu.ro/problems/service-unavailable";

Field Value

System.String

ProblemTypeUris.Unauthorized Field

Problem type URI for unauthenticated requests (HTTP 401).

public const string Unauthorized = "https://api.arolariu.ro/problems/unauthorized";

Field Value

System.String

ProblemTypeUris.Validation Field

Problem type URI for validation failures (HTTP 400).

public const string Validation = "https://api.arolariu.ro/problems/validation";

Field Value

System.String

// was this page useful?