Skip to content

Commit

Permalink
Add option to directly use HTTP status codes as failure reasons #34
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Mar 6, 2023
1 parent e0a5bfd commit 2749224
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions samples/Controllers/OperationResults.Sample/Program.cs
Expand Up @@ -29,6 +29,10 @@
// Adding new mappings or editing the existing ones allows to define what HTTP Status Codes the API must return
// for the Operation Results of our Business Logic methods.
options.StatusCodesMapping.Add(CustomFailureReasons.NotAvailable, StatusCodes.Status501NotImplemented);
// If you just want to directly use HTTP status codes as failure reasons, set the following property to true.
// In this way, the code you use with Result.Fail() will be used as response status code with no further mapping.
// options.UseHttpStatusCodes = true;
},
//updateModelStateResponseFactory: true);
// Passing a validation error default message or a validation error message provider automatically update the ModelStateResponseFactory.
Expand Down
4 changes: 4 additions & 0 deletions samples/MinimalApis/OperationResults.Sample/Program.cs
Expand Up @@ -28,6 +28,10 @@
// Adding new mappings or editing the existing ones allows to define what HTTP Status Codes the API must return
// for the Operation Results of our Business Logic methods.
options.StatusCodesMapping.Add(CustomFailureReasons.NotAvailable, StatusCodes.Status501NotImplemented);
// If you just want to directly use HTTP status codes as failure reasons, set the following property to true.
// In this way, the code you use with Result.Fail() will be used as response status code with no further mapping.
//options.UseHttpStatusCodes = true;
});

builder.Services.AddEndpointsApiExplorer();
Expand Down
Expand Up @@ -95,7 +95,7 @@ public static IResult ToResponse<T>(this Result<T> result, HttpContext httpConte
private static IResult Problem(HttpContext httpContext, int failureReason, object? content = null, string? title = null, string? detail = null, IEnumerable<ValidationError>? validationErrors = null)
{
var options = httpContext.RequestServices.GetService<OperationResultOptions>() ?? new OperationResultOptions();
var statusCode = options.GetStatusCode(failureReason);
var statusCode = options.UseHttpStatusCodes ? failureReason : options.GetStatusCode(failureReason);

if (content is not null)
{
Expand Down
Expand Up @@ -8,6 +8,8 @@ public class OperationResultOptions

public Dictionary<int, int> StatusCodesMapping { get; }

public bool UseHttpStatusCodes { get; set; }

public OperationResultOptions()
{
StatusCodesMapping = new Dictionary<int, int>
Expand Down
Expand Up @@ -87,7 +87,7 @@ public static IActionResult ToResponse<T>(this Result<T> result, HttpContext htt
private static IActionResult Problem(HttpContext httpContext, int failureReason, object? content = null, string? title = null, string? detail = null, IEnumerable<ValidationError>? validationErrors = null)
{
var options = httpContext.RequestServices.GetService<OperationResultOptions>() ?? new OperationResultOptions();
var statusCode = options.GetStatusCode(failureReason);
var statusCode = options.UseHttpStatusCodes ? failureReason : options.GetStatusCode(failureReason);

if (content is not null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/OperationResults.AspNetCore/OperationResultOptions.cs
Expand Up @@ -8,6 +8,8 @@ public class OperationResultOptions

public Dictionary<int, int> StatusCodesMapping { get; }

public bool UseHttpStatusCodes { get; set; }

public OperationResultOptions()
{
StatusCodesMapping = new Dictionary<int, int>
Expand Down

0 comments on commit 2749224

Please sign in to comment.