API Reference ResultExceptionThrowingExtensions - ulfbou/Zentient.Results GitHub Wiki

🚨 API Reference: static classZentient.Results.ResultExceptionThrowingExtensions

namespace Zentient.Results

📖 Summary

Provides an extension method to throw a ResultException when an IResult indicates failure. Facilitates exception-based error propagation bridging functional result patterns and imperative error handling.


📌 Metadata

Property Value
📦 Assembly Zentient.Results.dll
👁️ Visibility public
⚙️ Type Kind static class
📚 Category Result Flow Control
🧱 Dependencies IResult, ResultException

📂 Methods

🔹 ThrowIfFailure(this IResult result)

public static void ThrowIfFailure(this IResult result)

Throws a ResultException if the result is a failure, propagating the associated ErrorInfo collection.

Parameter Type Description
result IResult The result instance to check.

Exceptions

Exception Condition
ArgumentNullException If result is null.
ResultException If result.IsFailure is true.

🧠 Remarks

Use this method when:

  • Early exit with exception throwing is preferred.
  • You need immediate failure propagation in imperative flows.
  • Integrating functional result patterns with exception-based systems (e.g., ASP.NET Core middleware, legacy code, testing).

Avoid overuse in purely functional pipelines; favor it primarily in transitional or infrastructural code.


✅ Example Usage

public void HandleResult(IResult operationResult)
{
    operationResult.ThrowIfFailure();
    Console.WriteLine("Operation succeeded.");
}
public async Task<IActionResult> GetAsync()
{
    var result = await service.TryLoadAsync();
    result.ThrowIfFailure(); // Throws if failure

    return Ok(); // Only reached if success
}