Table of Contents

Class ExceptionExtensions

Namespace
Excalibur.Dispatch.Extensions
Assembly
Excalibur.Dispatch.dll

Provides extension methods for retrieving error and status codes from exceptions.

public static class ExceptionExtensions
Inheritance
ExceptionExtensions
Inherited Members

Remarks

Framework exceptions (ApiException and its derivatives, including DispatchException) are recognized by a direct type check -- never by probing for a conventionally-named property. Duck-typing a property name off an arbitrary exception type is the job IExceptionMapper already owns for exception types this framework does not itself define; this class does not compete with it.

Methods

GetErrorCode(Exception)

Attempts to retrieve the error code from an exception.

public static string? GetErrorCode(this Exception exception)

Parameters

exception Exception

The exception to extract the error code from.

Returns

string

The error code if found; otherwise, null . Returns the first error code found in this order:

  1. The ErrorCode of a dispatch exception (a direct type check -- never a property-name probe over an arbitrary exception type)
  2. ErrorCode entry in the exception's Data dictionary
  3. ErrorCode from inner exceptions (recursively)
  4. For AggregateException, the first non-null error code from inner exceptions.

Remarks

Error codes are text ( "MSG005" ), not numbers. An exception that types its own code as an integer still answers here -- the value is rendered as text rather than skipped. A foreign exception type this framework does not define is not probed for a conventionally-named property: route it through IExceptionMapper instead.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .

GetErrorCodeOrDefault(Exception, string)

Gets the error code from an exception with a default value if not found.

public static string GetErrorCodeOrDefault(this Exception exception, string defaultValue)

Parameters

exception Exception

The exception to extract the error code from.

defaultValue string

The default value to return if no error code is found.

Returns

string

The error code if found; otherwise, the specified default value.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .

GetStatusCode(Exception)

Attempts to retrieve the status code from an exception.

public static int? GetStatusCode(this Exception exception)

Parameters

exception Exception

The exception to extract the status code from.

Returns

int?

The status code if found; otherwise, null . Returns the first status code found in this order:

  1. DispatchStatusCode, when set
  2. StatusCode, for any ApiException (including a DispatchException that did not set DispatchStatusCode)
  3. StatusCode entry in the exception's Data dictionary
  4. StatusCode from inner exceptions (recursively).

Remarks

Both framework checks are direct type checks, never a property-name probe over an arbitrary exception type -- that is what made a ValidationException declaring 400 report as 500: reflection found the inherited StatusCode default before the more specific DispatchStatusCode was considered. A foreign exception type this framework does not define is not probed for a conventionally-named property: route it through IExceptionMapper instead.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .

GetStatusCodeOrDefault(Exception, int)

Gets the status code from an exception with a default value if not found.

public static int GetStatusCodeOrDefault(this Exception exception, int defaultValue = 500)

Parameters

exception Exception

The exception to extract the status code from.

defaultValue int

The default value to return if no status code is found.

Returns

int

The status code if found; otherwise, the specified default value.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .

HasErrorCode(Exception)

Checks if an exception has an error code.

public static bool HasErrorCode(this Exception exception)

Parameters

exception Exception

The exception to check.

Returns

bool

True if the exception has an error code; otherwise, false.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .

HasStatusCode(Exception)

Checks if an exception has a status code.

public static bool HasStatusCode(this Exception exception)

Parameters

exception Exception

The exception to check.

Returns

bool

True if the exception has a status code; otherwise, false.

Exceptions

ArgumentNullException

Thrown if the exception parameter is null .