API Reference ResultTryExtensions - ulfbou/Zentient.Results GitHub Wiki
Namespace:
Zentient.ResultsAssembly:Zentient.Results.dllAvailable since:v0.4.0
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
ErrorInfofailures, eliminating the need for explicittry/catchblocks in domain logic.
ResultTryExtensions encapsulates exception-handling logic inside a functional construct that returns a result. This promotes:
- ๐ก๏ธ Exception Safety: Eliminates scattered
try/catchblocks by wrapping risky code declaratively. - ๐ฏ Consistency: Converts any thrown
Exceptioninto a structuredErrorInfoviaResult.FromException(...). - ๐ Composability: Compatible with
Bind,Map, and other fluent operations in result pipelines. - ๐ Predictability: Always returns a valid
IResultorIResult<T>, allowing control flow to remain result-driven.
public static class ResultTryExtensions-
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
Exceptionis wrapped as anErrorInfowith metadata and returned as a failed result. -
Exceptions Thrown: None (exceptions are caught and converted).
-
Example Usage:
var result = (() => File.Delete(path)).Try();
-
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 withdefault(T). -
Exceptions Thrown: None (exceptions are caught and converted).
-
Example Usage:
var result = (() => File.ReadAllText("config.json")).Try();
var deleteResult = (() => File.Delete("temp.txt")).Try();
var readResult = (() => File.ReadAllText("data.json")).Try();
if (readResult.IsFailure)
Console.WriteLine(readResult.ErrorMessage);- Exceptions are never rethrown. All control flows remain within the
IResultpipeline. - Use
Tryto wrap any side-effecting operation prone to failure, especially in infrastructure layers (I/O, DB, HTTP). - Errors are internally modeled using
ErrorCodes.Exceptionand enriched with exception metadata (MetadataKeys).
| 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. |
ResultErrorInfoResultTransformationExtensionsMetadataKeysErrorCodes.Exception
#API #Try #ErrorHandling #ResultPattern #ExceptionSafety #ExtensionMethods #ZentientCore
Last Updated: 2025-06-22 Version: 0.4.0