Table of Contents

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

result Result<TIn>

The result to match on.

onSuccess Func<TIn, TOut>

Function to execute on success.

onValidation Func<ValidationError, TOut>

Function to execute when the error is a ValidationError.

onNotFound Func<NotFoundError, TOut>

Function to execute when the error is a NotFoundError.

onConflict Func<ConflictError, TOut>

Function to execute when the error is a ConflictError.

onBadRequest Func<BadRequestError, TOut>

Function to execute when the error is a BadRequestError.

onUnauthorized Func<UnauthorizedError, TOut>

Function to execute when the error is an UnauthorizedError.

onForbidden Func<ForbiddenError, TOut>

Function to execute when the error is a ForbiddenError.

onDomain Func<DomainError, TOut>

Function to execute when the error is a DomainError.

onRateLimit Func<RateLimitError, TOut>

Function to execute when the error is a RateLimitError.

onServiceUnavailable Func<ServiceUnavailableError, TOut>

Function to execute when the error is a ServiceUnavailableError.

onUnexpected Func<UnexpectedError, TOut>

Function to execute when the error is an UnexpectedError.

onError Func<Error, TOut>

Default function to execute for any other error type.

Returns

TOut

The output from the appropriate handler function.

Type Parameters

TIn

Type of the result value.

TOut

Type 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

result Result<TIn>

The result to switch on.

onSuccess Action<TIn>

Action to execute on success.

onValidation Action<ValidationError>

Action to execute when the error is a ValidationError.

onNotFound Action<NotFoundError>

Action to execute when the error is a NotFoundError.

onConflict Action<ConflictError>

Action to execute when the error is a ConflictError.

onBadRequest Action<BadRequestError>

Action to execute when the error is a BadRequestError.

onUnauthorized Action<UnauthorizedError>

Action to execute when the error is an UnauthorizedError.

onForbidden Action<ForbiddenError>

Action to execute when the error is a ForbiddenError.

onDomain Action<DomainError>

Action to execute when the error is a DomainError.

onRateLimit Action<RateLimitError>

Action to execute when the error is a RateLimitError.

onServiceUnavailable Action<ServiceUnavailableError>

Action to execute when the error is a ServiceUnavailableError.

onUnexpected Action<UnexpectedError>

Action to execute when the error is an UnexpectedError.

onError Action<Error>

Default action to execute for any other error type.

Type Parameters

TIn

Type 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.