Skip to content

Commit aec8879

Browse files
authored
Merge pull request #61 from marcominerva/develop
Update dependencies and refactor error handling
2 parents 1bcc19f + 936b6d5 commit aec8879

File tree

9 files changed

+45
-52
lines changed

9 files changed

+45
-52
lines changed

samples/Controllers/OperationResults.Sample/OperationResults.Sample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
11-
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.6" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
11+
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.9" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

samples/MinimalApis/OperationResults.Sample/OperationResults.Sample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
9+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

samples/OperationResults.Sample.BusinessLayer/Services/PeopleService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ public async Task<Result> DeleteAsync(Guid id)
123123
{
124124
var dbPerson = await dbContext.People.FirstOrDefaultAsync(p => p.Id == id);
125125

126-
if (dbPerson is not null)
126+
if (dbPerson is null)
127127
{
128-
if (dbPerson.FirstName == "Admin")
129-
{
130-
return Result.Fail(FailureReasons.Forbidden, "You cannot delete the Admin user");
131-
}
132-
133-
dbContext.Remove(dbPerson);
134-
await dbContext.SaveChangesAsync();
128+
return Result.Fail(FailureReasons.ItemNotFound);
129+
}
135130

136-
return Result.Ok();
131+
if (dbPerson.FirstName == "Admin")
132+
{
133+
return Result.Fail(FailureReasons.Forbidden, "You cannot delete the Admin user");
137134
}
138135

139-
return Result.Fail(FailureReasons.ItemNotFound);
136+
dbContext.Remove(dbPerson);
137+
await dbContext.SaveChangesAsync();
138+
139+
return Result.Ok();
140140
}
141141
}

samples/OperationResults.Sample.DataAccessLayer/OperationResults.Sample.DataAccessLayer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
1010
</ItemGroup>
1111
</Project>

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139" PrivateAssets="All" />
12+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.143" PrivateAssets="All" />
1313
</ItemGroup>
1414

1515
</Project>

src/OperationResults.AspNetCore.Http/HttpContextExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static class HttpContextExtensions
77
public static IResult CreateResponse(this HttpContext httpContext, Result result, int? successStatusCode = null)
88
=> result.ToResponse(httpContext, successStatusCode);
99

10-
public static IResult CreateResponse(this HttpContext httpContext, Result result, string? routeName, object? routeValues = null, int? successStatusCode = null)
11-
=> result.ToResponse(httpContext, routeName, routeValues, successStatusCode);
10+
public static IResult CreateResponse(this HttpContext httpContext, Result result, string? routeName, object? routeValues = null)
11+
=> result.ToResponse(httpContext, routeName, routeValues);
1212

1313
public static IResult CreateResponse<T>(this HttpContext httpContext, Result<T> result, int? successStatusCode = null)
1414
=> result.ToResponse(httpContext, null, null, successStatusCode);

src/OperationResults.AspNetCore.Http/OperationResultExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IResult ToResponse(this Result result, HttpContext httpContext, in
2323
return Problem(httpContext, result.FailureReason, null, result.ErrorMessage, result.ErrorDetail, result.ValidationErrors);
2424
}
2525

26-
public static IResult ToResponse(this Result result, HttpContext httpContext, string? routeName, object? routeValues = null, int? successStatusCode = null)
26+
public static IResult ToResponse(this Result result, HttpContext httpContext, string? routeName, object? routeValues = null)
2727
{
2828
if (result.Success)
2929
{
@@ -109,7 +109,6 @@ private static IResult Problem(HttpContext httpContext, int failureReason, objec
109109
var problemDetails = new ProblemDetails
110110
{
111111
Status = statusCode,
112-
Type = $"https://httpstatuses.io/{statusCode}",
113112
Title = title ?? ReasonPhrases.GetReasonPhrase(statusCode),
114113
Detail = detail,
115114
Instance = httpContext.Request.Path
@@ -131,9 +130,12 @@ private static IResult Problem(HttpContext httpContext, int failureReason, objec
131130
}
132131

133132
#if NET6_0
134-
return Results.Json(problemDetails, statusCode: statusCode, contentType: "application/problem+json; charset=utf-8");
133+
return Results.Problem(problemDetails);
135134
#else
136-
return TypedResults.Json(problemDetails, statusCode: statusCode, contentType: "application/problem+json; charset=utf-8");
135+
var problemResult = TypedResults.Problem(problemDetails);
136+
problemResult.ProblemDetails.Type ??= $"https://httpstatuses.io/{statusCode}";
137+
138+
return problemResult;
137139
#endif
138140
}
139141
}

src/OperationResults.AspNetCore/OperationResultExtensions.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Diagnostics;
2-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
32
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.Infrastructure;
44
using Microsoft.AspNetCore.WebUtilities;
55
using Microsoft.Extensions.DependencyInjection;
66

@@ -99,16 +99,10 @@ private static IActionResult Problem(HttpContext httpContext, int failureReason,
9999
return objectResult;
100100
}
101101

102-
var problemDetails = new ProblemDetails
103-
{
104-
Status = statusCode,
105-
Type = $"https://httpstatuses.io/{statusCode}",
106-
Title = title ?? ReasonPhrases.GetReasonPhrase(statusCode),
107-
Detail = detail,
108-
Instance = httpContext.Request.Path
109-
};
110-
111-
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
102+
var problemDetailsFactory = httpContext.RequestServices.GetRequiredService<ProblemDetailsFactory>();
103+
var problemDetails = problemDetailsFactory.CreateProblemDetails(httpContext, statusCode, title ?? ReasonPhrases.GetReasonPhrase(statusCode),
104+
detail: detail, instance: httpContext.Request.Path);
105+
problemDetails.Type ??= $"https://httpstatuses.io/{statusCode}";
112106

113107
if (validationErrors?.Any() ?? false)
114108
{
@@ -123,12 +117,12 @@ private static IActionResult Problem(HttpContext httpContext, int failureReason,
123117
}
124118
}
125119

126-
var problemDetailsResults = new JsonResult(problemDetails)
120+
var problemDetailsResult = new JsonResult(problemDetails)
127121
{
128122
StatusCode = statusCode,
129-
ContentType = "application/problem+json; charset=utf-8"
123+
ContentType = "application/problem+json"
130124
};
131125

132-
return problemDetailsResults;
126+
return problemDetailsResult;
133127
}
134128
}

src/OperationResults.AspNetCore/ServiceCollectionExtensions.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Diagnostics;
2-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
32
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.Infrastructure;
44
using Microsoft.AspNetCore.Mvc.ModelBinding;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -45,15 +45,11 @@ private static IServiceCollection AddOperationResult(this IServiceCollection ser
4545
{
4646
var httpContext = actionContext.HttpContext;
4747
var statusCode = operationResultOptions.GetStatusCode(FailureReasons.ClientError, StatusCodes.Status400BadRequest);
48-
var problemDetails = new ProblemDetails
49-
{
50-
Status = statusCode,
51-
Title = validationErrorMessageProvider?.Invoke(actionContext.ModelState) ?? "One or more validation errors occurred",
52-
Type = $"https://httpstatuses.io/{statusCode}",
53-
Instance = httpContext.Request.Path
54-
};
5548

56-
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
49+
var problemDetailsFactory = httpContext.RequestServices.GetRequiredService<ProblemDetailsFactory>();
50+
var problemDetails = problemDetailsFactory.CreateProblemDetails(httpContext, statusCode, validationErrorMessageProvider?.Invoke(actionContext.ModelState) ?? "One or more validation errors occurred",
51+
instance: httpContext.Request.Path);
52+
problemDetails.Type ??= $"https://httpstatuses.io/{statusCode}";
5753

5854
if (operationResultOptions.ErrorResponseFormat == ErrorResponseFormat.Default)
5955
{
@@ -72,12 +68,13 @@ private static IServiceCollection AddOperationResult(this IServiceCollection ser
7268
problemDetails.Extensions.Add("errors", errors);
7369
}
7470

75-
var result = new ObjectResult(problemDetails)
71+
var problemDetailsResult = new JsonResult(problemDetails)
7672
{
77-
StatusCode = statusCode
73+
StatusCode = statusCode,
74+
ContentType = "application/problem+json"
7875
};
7976

80-
return result;
77+
return problemDetailsResult;
8178
};
8279
});
8380
}

0 commit comments

Comments
 (0)