API Reference ResultTryExtensions - ulfbou/Zentient.Results GitHub Wiki

๐Ÿ›ก๏ธ API Reference: static class โ€“ ResultTryExtensions

Namespace: Zentient.Results Assembly: Zentient.Results.dll Available since: v0.4.0


๐Ÿ“– Summary

Provides extension methods for safely executing operations that may throw exceptions, returning standardized IResult or IResult<T> objects.

This class enables defensive programming patterns by automatically converting exceptions into structured ErrorInfo failures, eliminating the need for explicit try/catch blocks in domain logic.


๐Ÿ’ก Design Philosophy / Rationale

ResultTryExtensions encapsulates exception-handling logic inside a functional construct that returns a result. This promotes:

  • ๐Ÿ›ก๏ธ Exception Safety: Eliminates scattered try/catch blocks by wrapping risky code declaratively.
  • ๐ŸŽฏ Consistency: Converts any thrown Exception into a structured ErrorInfo via Result.FromException(...).
  • ๐Ÿ“‹ Composability: Compatible with Bind, Map, and other fluent operations in result pipelines.
  • ๐Ÿ” Predictability: Always returns a valid IResult or IResult<T>, allowing control flow to remain result-driven.

๐Ÿ”– Type Signature

public static class ResultTryExtensions

๐Ÿ”ง Methods

1. Try(...) (Action)

  • Signature:

    public static IResult Try(this Action action)
  • Summary: Executes a non-returning action and captures any thrown exception as a failure result.

  • Parameters:

    • action (this Action): The operation to execute.
  • Return Value: IResult โ€“ success if no exception is thrown; failure otherwise.

  • Behavior: If the action throws, its Exception is wrapped as an ErrorInfo with metadata and returned as a failed result.

  • Exceptions Thrown: None (exceptions are caught and converted).

  • Example Usage:

    var result = (() => File.Delete(path)).Try();

2. Try<T>(...) (Func<T>)

  • Signature:

    public static IResult<T> Try<T>(this Func<T> func)
  • Summary: Executes a function and captures the return value or exception as a result.

  • Parameters:

    • func (this Func<T>): The value-returning operation to execute.
  • Return Value: IResult<T> โ€“ the function result if successful; failure if an exception occurs.

  • Behavior: If the function completes, its result is returned as Result<T>.Success(value); if it throws, the exception is captured and returned as failure with default(T).

  • Exceptions Thrown: None (exceptions are caught and converted).

  • Example Usage:

    var result = (() => File.ReadAllText("config.json")).Try();

๐Ÿงช Usage Examples

var deleteResult = (() => File.Delete("temp.txt")).Try();

var readResult = (() => File.ReadAllText("data.json")).Try();
if (readResult.IsFailure)
    Console.WriteLine(readResult.ErrorMessage);

โš ๏ธ Remarks

  • Exceptions are never rethrown. All control flows remain within the IResult pipeline.
  • Use Try to wrap any side-effecting operation prone to failure, especially in infrastructure layers (I/O, DB, HTTP).
  • Errors are internally modeled using ErrorCodes.Exception and enriched with exception metadata (MetadataKeys).

๐Ÿงฉ Integration and Interoperability

Layer Integration Pattern
CQRS Handlers Use to guard unsafe domain/infrastructure interactions.
Telemetry Enrich captured exceptions via ITelemetryEnricher.
Result Factories Delegates into Result.FromException(...) internally.
ErrorInfo Converts exceptions into full metadata-rich structured errors.

๐Ÿ“š See Also

  • Result
  • ErrorInfo
  • ResultTransformationExtensions
  • MetadataKeys
  • ErrorCodes.Exception

๐Ÿท๏ธ Tags

#API #Try #ErrorHandling #ResultPattern #ExceptionSafety #ExtensionMethods #ZentientCore


Last Updated: 2025-06-22 Version: 0.4.0

โš ๏ธ **GitHub.com Fallback** โš ๏ธ