Class ExceptionError
Wraps a .NET Exception as a typed error reason. Automatically captures the exception type, stack trace, and inner exception message as tags. Produced automatically by Try(Action, Func<Exception, IError>?) and TryAsync(Func<Task>, Func<Exception, IError>?, CancellationToken).
public class ExceptionError : Reason<ExceptionError>, IError, IReason
- Inheritance
-
ExceptionError
- Implements
- Inherited Members
- Extension Methods
Examples
var result = Result<string>.Try(() => File.ReadAllText("config.json"));
if (result.IsFailure)
{
var ex = result.Errors.OfType<ExceptionError>().First();
Console.WriteLine(ex.Exception.Message); // original exception message
Console.WriteLine(ex.Tags["ExceptionType"]); // e.g. "FileNotFoundException"
}
Constructors
ExceptionError(Exception)
Creates an error from an exception, using the exception's own message.
public ExceptionError(Exception exception)
Parameters
exceptionExceptionThe exception to wrap. Must not be null.
ExceptionError(string, Exception)
Creates an error with a custom message, preserving the original exception for diagnostics.
public ExceptionError(string message, Exception exception)
Parameters
messagestringCustom human-readable error message.
exceptionExceptionThe exception to wrap. Must not be null.
Properties
Exception
Gets the original .NET exception that caused this error.
public Exception Exception { get; init; }
Property Value
Methods
CreateNew(string, ImmutableDictionary<string, object>)
Factory method for creating new instances (maintains immutability). Must be implemented by derived classes.
protected override ExceptionError CreateNew(string message, ImmutableDictionary<string, object> tags)
Parameters
messagestringtagsImmutableDictionary<string, object>