This project contains extension method to convert Sisusa.ReturnTypes.Failure to an ActionResult to simplify result handling in web api projects.
$ dotnet add package Sisusa.ReturnTypes.ToActionResultSisusa.ReturnTypes.WebApiThis namespace contains classes and extensions to handle failures in a Web API by converting them into appropriate HTTP responses using ActionResult.
I would have loved to have this ship as part of the Sisusa.Common.RetunTypes package but hated cluttering up that package's dependency chain - this package has an obvious dependency on Microsoft.ApnetCore.Mvc and the Sisusa packages (Data.Contracts, ReturnTypes)
FailureExtensionsWebApiA static class providing extension methods for converting IFailure objects into ASP.NET Core ActionResult instances.
ToActionResult(this IFailure failure)Converts an IFailure object into an ActionResult representing the appropriate HTTP response.
failure (IFailure): An object representing a failure. If null, a generic "No failure to convert." message is returned.ActionResult:
ProblemDetails object wrapped in ObjectResult, containing the HTTP status code, failure message, and details.failure is null.InnerException from the failure object or assigns a generic exception message.ProblemDetails object with:
GetStatusCodeFromException.failure.GetMessageDetailsFromException.ProblemDetails object wrapped in an ObjectResult.GetMessageDetailsFromException(Exception ex)Retrieves detailed information from an exception.
ex (Exception): The exception to extract details from.string: A detailed description of the exception including:
DEBUG is enabled):
GetStatusCodeFromException(Exception ex)Maps an exception type to an appropriate HTTP status code.
ex (Exception): The exception to evaluate.int: The HTTP status code corresponding to the exception.EntityNotFoundException: 404 Not FoundBadConnectionException: 503 Service UnavailableUnauthorizedAccessException: 401 UnauthorizedDuplicateKeyException: 409 ConflictArgumentException: 400 Bad RequestNullReferenceException: 400 Bad RequestProblemDetails).GetStatusCodeFromException to map specific exceptions to HTTP status codes.GetStatusCodeFromException.var failure = new CustomFailure("Something went wrong.");
var result = failure.ToActionResult();
// Use 'result' as the response in a controller action.
return result;
Most probable use case - the use case I wrote the library for.
[HttpGet("/{id:int}")]
public ActionResult<User> GetOne([FromRoute]int id)
{
return userService
.FindById(id)
.MatchReturn(
user=>Ok(user),
err => err.ToActionResult()
);
}
This documentation provides an overview of the code's purpose, functionality, and usage to assist developers in understanding and utilizing the FailureExtensionsWebApi class effectively.