Class NanoRouteExceptionExtensions

Adds helpers for normalizing exceptions and extracting structured error details.

Inheritance
object
NanoRouteExceptionExtensions
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public static class NanoRouteExceptionExtensions

Fields

DeveloperMessagesName

The Data key used to store developer-facing diagnostic details.

Declaration
public const string DeveloperMessagesName = "DeveloperMessages"
Field Value
Type Description
string
Remarks

Written by Throw(HttpStatusCode, string, Exception?, IEnumerable<string>?, IEnumerable<string>?) and read by GetErrorDetails(HttpRequestException, bool, string?).

ErrorsName

The Data key used to store client-facing error messages.

Declaration
public const string ErrorsName = "Errors"
Field Value
Type Description
string
Remarks

Written by Throw(HttpStatusCode, string, Exception?, IEnumerable<string>?, IEnumerable<string>?) and read by GetErrorDetails(HttpRequestException, bool, string?).

StatusName

The Data key used to store the HTTP status code.

Declaration
public const string StatusName = "StatusCode"
Field Value
Type Description
string
Remarks

Written by Throw(HttpStatusCode, string, Exception?, IEnumerable<string>?, IEnumerable<string>?) and read by GetErrorDetails(HttpRequestException, bool, string?).

Methods

AddExceptionHandler<TBuilder>(TBuilder)

Adds an exception-handling middleware for all supported HTTP methods.

Declaration
public static TBuilder AddExceptionHandler<TBuilder>(this TBuilder routeBuilder) where TBuilder : notnull, RouteBuilder
Parameters
Type Name Description
TBuilder routeBuilder
Returns
Type Description
TBuilder

The current routeBuilder instance.

Type Parameters
Name Description
TBuilder
Remarks

The inserted middleware converts unexpected exceptions into HttpRequestException values with normalized status codes and diagnostic payloads. Existing HttpRequestException values are allowed to flow through unchanged. OperationCanceledException is intentionally not normalized so caller-driven cancellation can propagate unchanged.

AddExceptionHandler<TBuilder>(TBuilder, IEnumerable<string>)

Adds an exception-handling middleware for the selected HTTP methods.

Declaration
public static TBuilder AddExceptionHandler<TBuilder>(this TBuilder routeBuilder, IEnumerable<string> verbs) where TBuilder : notnull, RouteBuilder
Parameters
Type Name Description
TBuilder routeBuilder
IEnumerable<string> verbs

The HTTP methods that should use the exception-handling middleware.

Returns
Type Description
TBuilder

The current routeBuilder instance.

Type Parameters
Name Description
TBuilder
Remarks

The inserted middleware converts unexpected exceptions into HttpRequestException values with normalized status codes and diagnostic payloads. Existing HttpRequestException values are allowed to flow through unchanged. OperationCanceledException is intentionally not normalized so caller-driven cancellation can propagate unchanged.

AddExceptionHandler<TBuilder>(TBuilder, IEnumerable<string>, string)

Adds an exception-handling middleware for the selected HTTP methods.

Declaration
public static TBuilder AddExceptionHandler<TBuilder>(this TBuilder routeBuilder, IEnumerable<string> verbs, string pattern) where TBuilder : notnull, RouteBuilder
Parameters
Type Name Description
TBuilder routeBuilder
IEnumerable<string> verbs

The HTTP methods that should use the exception-handling middleware.

string pattern

The route pattern where the exception-handling middleware should be inserted. Use / to apply it to the whole pipeline, or a narrower prefix/exact pattern to scope normalization to selected routes.

Returns
Type Description
TBuilder

The current routeBuilder instance.

Type Parameters
Name Description
TBuilder
Remarks

The inserted middleware converts unexpected exceptions into HttpRequestException values with normalized status codes and diagnostic payloads. Existing HttpRequestException values are allowed to flow through unchanged. OperationCanceledException is intentionally not normalized so caller-driven cancellation can propagate unchanged.

AddExceptionHandler<TBuilder>(TBuilder, string)

Adds an exception-handling middleware for all supported HTTP methods.

Declaration
public static TBuilder AddExceptionHandler<TBuilder>(this TBuilder routeBuilder, string pattern) where TBuilder : notnull, RouteBuilder
Parameters
Type Name Description
TBuilder routeBuilder
string pattern

The route pattern where the exception-handling middleware should be inserted. Use / to apply it to the whole pipeline, or a narrower prefix/exact pattern to scope normalization to selected routes.

Returns
Type Description
TBuilder

The current routeBuilder instance.

Type Parameters
Name Description
TBuilder
Remarks

The inserted middleware converts unexpected exceptions into HttpRequestException values with normalized status codes and diagnostic payloads. Existing HttpRequestException values are allowed to flow through unchanged. OperationCanceledException is intentionally not normalized so caller-driven cancellation can propagate unchanged.

AddExceptionHandler<TBuilder>(TBuilder, string, string)

Adds an exception-handling middleware for a single HTTP method.

Declaration
public static TBuilder AddExceptionHandler<TBuilder>(this TBuilder routeBuilder, string verb, string pattern) where TBuilder : notnull, RouteBuilder
Parameters
Type Name Description
TBuilder routeBuilder
string verb

The HTTP method that should use the exception-handling middleware.

string pattern

The route pattern where the exception-handling middleware should be inserted. Use / to apply it to the whole pipeline, or a narrower prefix/exact pattern to scope normalization to selected routes.

Returns
Type Description
TBuilder

The current routeBuilder instance.

Type Parameters
Name Description
TBuilder
Remarks

The inserted middleware converts unexpected exceptions into HttpRequestException values with normalized status codes and diagnostic payloads. Existing HttpRequestException values are allowed to flow through unchanged. OperationCanceledException is intentionally not normalized so caller-driven cancellation can propagate unchanged.

GetErrorDetails(HttpRequestException, bool, string?)

Converts an HttpRequestException into an ErrorDetails payload.

Declaration
public static ErrorDetails GetErrorDetails(this HttpRequestException requestException, bool populateErrorInfo = false, string? traceId = null)
Parameters
Type Name Description
HttpRequestException requestException
bool populateErrorInfo

true to include developer-facing details when present; otherwise false.

string traceId

The trace identifier to expose in the resulting payload.

Returns
Type Description
ErrorDetails

The structured error payload.

Throw(HttpStatusCode, string, params IEnumerable<string>)

Throws an HttpRequestException enriched with an HTTP status code and public error messages.

Declaration
[DoesNotReturn]
public static void Throw(HttpStatusCode status, string title, params IEnumerable<string> errors)
Parameters
Type Name Description
HttpStatusCode status

The HTTP status code that should be associated with the exception.

string title

The human-readable error title.

IEnumerable<string> errors

Optional client-facing error messages that should not contain sensitive data.

Throw(HttpStatusCode, string, Exception?, IEnumerable<string>?, IEnumerable<string>?)

Throws an HttpRequestException enriched with routing-specific metadata.

Declaration
[DoesNotReturn]
public static void Throw(HttpStatusCode status, string title, Exception? original = null, IEnumerable<string>? errors = null, IEnumerable<string>? developerMessages = null)
Parameters
Type Name Description
HttpStatusCode status

The HTTP status code that should be associated with the exception.

string title

The human-readable error title.

Exception original

The original exception, if any.

IEnumerable<string> errors

Optional client-facing error messages that should not contain sensitive data.

IEnumerable<string> developerMessages

Optional developer-facing messages that may contain sensitive data.

In this article
Back to top Generated by DocFX