Struct SystemFailure
public readonly struct SystemFailure : IResultFailure
- Implements
- Inherited Members
Examples
try
{
// Some operation that might throw
}
catch (Exception ex)
{
SystemFailure failure = SystemFailure.FromException(ex);
Result<int> result = failure.ToResult<int>();
}
Properties
Type
The type of failure.
public Type Type { get; }
Property Value
Methods
FromException(Exception)
Creates a failure from an exception.
public static SystemFailure FromException(Exception exception)
Parameters
exceptionExceptionThe exception.
Returns
- SystemFailure
The failure.
Examples
var exception = new InvalidOperationException("Connection timeout");
SystemFailure failure = SystemFailure.FromException(exception);
Console.WriteLine(failure.GetFailureMessage()); // "Connection timeout"
GetFailureMessage()
Returns the error message defined in the failure.
public string GetFailureMessage()
Returns
- string
The error message.
Examples
IResultFailure failure = ResultFailure.FromErrorMessage("Invalid input");
string message = failure.GetFailureMessage(); // "Invalid input"
ToException()
Converts the failure to an exception.
public Exception ToException()
Returns
- Exception
The exception.
Examples
IResultFailure failure = ResultFailure.FromErrorMessage("Operation failed");
throw failure.ToException();
ToResult<T>()
Converts the failure to a Result with a Failure state.
public Result<T> ToResult<T>()
Returns
- Result<T>
A Result with a Failure state.
Type Parameters
TThe underlying type of Result.
Examples
IResultFailure failure = ResultFailure.FromErrorMessage("Not found");
Result<User> result = failure.ToResult<User>();
Console.WriteLine(result.IsFailure); // true