Class Result
- Namespace
- FoundationaLLM.Common.Models.Services
- Assembly
- FoundationaLLM.Common.dll
Represents the outcome of an operation, indicating either success or failure and providing error information if applicable.
public class Result
- Inheritance
-
Result
- Inherited Members
- Extension Methods
Remarks
Use the Result class to encapsulate the success or failure state of an operation without relying on exceptions for control flow. When a failure occurs, the associated error can be accessed through the Error property. For results that carry a value on success, use the generic Result<T> type.
Properties
Error
Gets the error associated with a failure result.
[JsonPropertyName("error")]
public DomainError? Error { get; }
Property Value
IsFailure
Gets a value indicating whether the result represents a failure state.
[JsonIgnore]
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether the result refers to a successful outcome.
[JsonPropertyName("is_success")]
public bool IsSuccess { get; }
Property Value
Methods
Failure(DomainError)
Creates a failed result with the specified domain error.
public static Result Failure(DomainError error)
Parameters
errorDomainErrorThe domain error that describes the reason for the failure. Cannot be null.
Returns
FailureFromErrorMessage(string, int, string?)
Creates a failed result with the specified error message.
public static Result FailureFromErrorMessage(string errorMessage, int status = 500, string? instance = null)
Parameters
errorMessagestringThe error message that describes the reason for the failure. Cannot be null or empty.
statusintThe status code associated with the exception.
instancestringAn optional instance identifier for the error.
Returns
- Result
A failed result containing the specified error message.
FailureFromException(Exception, int, string?)
Creates a failed result that represents the specified exception.
public static Result FailureFromException(Exception exception, int status = 500, string? instance = null)
Parameters
exceptionExceptionThe exception to convert into a failure result. Cannot be null.
statusintThe status code associated with the exception.
instancestringAn optional instance identifier for the error.
Returns
- Result
A failed result containing error information derived from the specified exception.
FailureFromHttpResponse(HttpResponseMessage)
Creates a failed result based on the error information extracted from the specified HTTP response.
public static Task<Result> FailureFromHttpResponse(HttpResponseMessage response)
Parameters
responseHttpResponseMessageThe HTTP response message from which to extract error details. Cannot be null.
Returns
- Task<Result>
A task that represents the asynchronous operation. The task result contains a failed result populated with error information from the HTTP response.
Success()
Creates a successful result.
public static Result Success()
Returns
ToActionResult()
Converts the current result to an appropriate ActionResult for use in ASP.NET Core MVC controllers.
public ActionResult ToActionResult()
Returns
- ActionResult
An OkResult if the operation was successful; otherwise, an ObjectResult containing the ProblemDetails object with error information.
Remarks
Use this method to translate the result of an operation into a standardized HTTP response for controller actions. The returned result can be returned directly from an action method.