Class MatchErrorExtensions
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Pattern matching helpers for discriminating specific error types in Result. Allows matching on specific error types (ValidationError, NotFoundError, etc.) rather than treating all errors the same.
public static class MatchErrorExtensions
- Inheritance
-
MatchErrorExtensions
- Inherited Members
Methods
MatchError<TIn, TOut>(Result<TIn>, Func<TIn, TOut>, Func<ValidationError, TOut>?, Func<NotFoundError, TOut>?, Func<ConflictError, TOut>?, Func<BadRequestError, TOut>?, Func<UnauthorizedError, TOut>?, Func<ForbiddenError, TOut>?, Func<DomainError, TOut>?, Func<RateLimitError, TOut>?, Func<ServiceUnavailableError, TOut>?, Func<UnexpectedError, TOut>?, Func<Error, TOut>?)
Pattern matches on the result, with specific handlers for different error types.
public static TOut MatchError<TIn, TOut>(this Result<TIn> result, Func<TIn, TOut> onSuccess, Func<ValidationError, TOut>? onValidation = null, Func<NotFoundError, TOut>? onNotFound = null, Func<ConflictError, TOut>? onConflict = null, Func<BadRequestError, TOut>? onBadRequest = null, Func<UnauthorizedError, TOut>? onUnauthorized = null, Func<ForbiddenError, TOut>? onForbidden = null, Func<DomainError, TOut>? onDomain = null, Func<RateLimitError, TOut>? onRateLimit = null, Func<ServiceUnavailableError, TOut>? onServiceUnavailable = null, Func<UnexpectedError, TOut>? onUnexpected = null, Func<Error, TOut>? onError = null)
Parameters
resultResult<TIn>The result to match on.
onSuccessFunc<TIn, TOut>Function to execute on success.
onValidationFunc<ValidationError, TOut>Function to execute when the error is a ValidationError.
onNotFoundFunc<NotFoundError, TOut>Function to execute when the error is a NotFoundError.
onConflictFunc<ConflictError, TOut>Function to execute when the error is a ConflictError.
onBadRequestFunc<BadRequestError, TOut>Function to execute when the error is a BadRequestError.
onUnauthorizedFunc<UnauthorizedError, TOut>Function to execute when the error is an UnauthorizedError.
onForbiddenFunc<ForbiddenError, TOut>Function to execute when the error is a ForbiddenError.
onDomainFunc<DomainError, TOut>Function to execute when the error is a DomainError.
onRateLimitFunc<RateLimitError, TOut>Function to execute when the error is a RateLimitError.
onServiceUnavailableFunc<ServiceUnavailableError, TOut>Function to execute when the error is a ServiceUnavailableError.
onUnexpectedFunc<UnexpectedError, TOut>Function to execute when the error is an UnexpectedError.
onErrorFunc<Error, TOut>Default function to execute for any other error type.
Returns
- TOut
The output from the appropriate handler function.
Type Parameters
TInType of the result value.
TOutType of the output.
Examples
var message = GetUser(userId).MatchError(
onSuccess: user => $"Found: {user.Name}",
onNotFound: err => "User not found",
onValidation: err => $"Invalid: {err.Detail}",
onError: err => "An error occurred"
);
Remarks
Error handlers are evaluated in order. The first matching error type handler is executed. If no specific error type matches and onError is not provided, an InvalidOperationException is thrown.
SwitchError<TIn>(Result<TIn>, Action<TIn>, Action<ValidationError>?, Action<NotFoundError>?, Action<ConflictError>?, Action<BadRequestError>?, Action<UnauthorizedError>?, Action<ForbiddenError>?, Action<DomainError>?, Action<RateLimitError>?, Action<ServiceUnavailableError>?, Action<UnexpectedError>?, Action<Error>?)
Executes different actions based on the result state and error type.
public static void SwitchError<TIn>(this Result<TIn> result, Action<TIn> onSuccess, Action<ValidationError>? onValidation = null, Action<NotFoundError>? onNotFound = null, Action<ConflictError>? onConflict = null, Action<BadRequestError>? onBadRequest = null, Action<UnauthorizedError>? onUnauthorized = null, Action<ForbiddenError>? onForbidden = null, Action<DomainError>? onDomain = null, Action<RateLimitError>? onRateLimit = null, Action<ServiceUnavailableError>? onServiceUnavailable = null, Action<UnexpectedError>? onUnexpected = null, Action<Error>? onError = null)
Parameters
resultResult<TIn>The result to switch on.
onSuccessAction<TIn>Action to execute on success.
onValidationAction<ValidationError>Action to execute when the error is a ValidationError.
onNotFoundAction<NotFoundError>Action to execute when the error is a NotFoundError.
onConflictAction<ConflictError>Action to execute when the error is a ConflictError.
onBadRequestAction<BadRequestError>Action to execute when the error is a BadRequestError.
onUnauthorizedAction<UnauthorizedError>Action to execute when the error is an UnauthorizedError.
onForbiddenAction<ForbiddenError>Action to execute when the error is a ForbiddenError.
onDomainAction<DomainError>Action to execute when the error is a DomainError.
onRateLimitAction<RateLimitError>Action to execute when the error is a RateLimitError.
onServiceUnavailableAction<ServiceUnavailableError>Action to execute when the error is a ServiceUnavailableError.
onUnexpectedAction<UnexpectedError>Action to execute when the error is an UnexpectedError.
onErrorAction<Error>Default action to execute for any other error type.
Type Parameters
TInType of the result value.
Remarks
Error handlers are evaluated in order. The first matching error type handler is executed. If no specific error type matches and onError is not provided, an InvalidOperationException is thrown.