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
exceptionExceptionThe 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:- The ErrorCode of a dispatch exception (a direct type check -- never a property-name probe over an arbitrary exception type)
- ErrorCode entry in the exception's Data dictionary
- ErrorCode from inner exceptions (recursively)
- 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
exceptionparameter isnull.
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
exceptionExceptionThe exception to extract the error code from.
defaultValuestringThe 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
exceptionparameter isnull.
GetStatusCode(Exception)
Attempts to retrieve the status code from an exception.
public static int? GetStatusCode(this Exception exception)
Parameters
exceptionExceptionThe 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:- DispatchStatusCode, when set
- StatusCode, for any ApiException (including a DispatchException that did not set DispatchStatusCode)
- StatusCode entry in the exception's Data dictionary
- 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
exceptionparameter isnull.
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
exceptionExceptionThe exception to extract the status code from.
defaultValueintThe 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
exceptionparameter isnull.
HasErrorCode(Exception)
Checks if an exception has an error code.
public static bool HasErrorCode(this Exception exception)
Parameters
exceptionExceptionThe exception to check.
Returns
- bool
True if the exception has an error code; otherwise, false.
Exceptions
- ArgumentNullException
Thrown if the
exceptionparameter isnull.
HasStatusCode(Exception)
Checks if an exception has a status code.
public static bool HasStatusCode(this Exception exception)
Parameters
exceptionExceptionThe exception to check.
Returns
- bool
True if the exception has a status code; otherwise, false.
Exceptions
- ArgumentNullException
Thrown if the
exceptionparameter isnull.