Skip to content
This repository was archived by the owner on Dec 2, 2023. It is now read-only.

Commit 7e73674

Browse files
committed
Allow users to add error details during manual response creation
1 parent 80cd832 commit 7e73674

File tree

13 files changed

+84
-73
lines changed

13 files changed

+84
-73
lines changed

ExceptionAll.APIExample/ExceptionAll.APIExample.csproj

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

77
<ItemGroup>
8-
<PackageReference Include="FluentValidation" Version="10.3.0" />
9-
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.0" />
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.5" />
8+
<PackageReference Include="FluentValidation" Version="10.3.5" />
9+
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.5" />
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

ExceptionAll.Tests/ExceptionAll.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
1313
<PackageReference Include="Moq" Version="4.16.1" />
1414
<PackageReference Include="xunit" Version="2.4.1" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

ExceptionAll/Details/BadRequestDetails.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
public class BadRequestDetails : BaseDetails
44
{
5-
public BadRequestDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
5+
public BadRequestDetails(ActionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
66
base(
77
context ?? throw new ArgumentNullException(nameof(context)),
88
string.IsNullOrEmpty(title) ? "Bad Request" : title,
99
context.HttpContext.Request.Path,
1010
StatusCodes.Status400BadRequest,
1111
string.IsNullOrEmpty (message) ? "See errors or logs for more details" : message,
12-
errors)
12+
errors ?? new List<ErrorDetail>())
1313
{
1414
}
15-
public BadRequestDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
15+
public BadRequestDetails(ExceptionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
1616
base(
1717
context ?? throw new ArgumentNullException(nameof(context)),
1818
string.IsNullOrEmpty(title) ? "Bad Request" : title,
1919
context.HttpContext.Request.Path,
2020
StatusCodes.Status400BadRequest,
2121
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22-
errors)
22+
errors ?? new List<ErrorDetail>())
2323
{
2424
}
2525
}

ExceptionAll/Details/ForbiddenDetails.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
public class ForbiddenDetails : BaseDetails
44
{
5-
public ForbiddenDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
5+
public ForbiddenDetails(ActionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
66
base(
77
context ?? throw new ArgumentNullException(nameof(context)),
88
string.IsNullOrEmpty(title) ? "Forbidden" : title,
99
context.HttpContext.Request.Path,
1010
StatusCodes.Status403Forbidden,
1111
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12-
errors)
12+
errors ?? new List<ErrorDetail>())
1313
{
1414
}
15-
public ForbiddenDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
15+
public ForbiddenDetails(ExceptionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
1616
base(
1717
context ?? throw new ArgumentNullException(nameof(context)),
1818
string.IsNullOrEmpty(title) ? "Forbidden" : title,
1919
context.HttpContext.Request.Path,
2020
StatusCodes.Status403Forbidden,
2121
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22-
errors)
22+
errors ?? new List<ErrorDetail>())
2323
{
2424
}
2525
}

ExceptionAll/Details/InternalServerErrorDetails.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
public class InternalServerErrorDetails : BaseDetails
44
{
5-
public InternalServerErrorDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
5+
public InternalServerErrorDetails(ActionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
66
base(
77
context ?? throw new ArgumentNullException(nameof(context)),
88
string.IsNullOrEmpty(title) ? "Internal Server Error" : title,
99
context.HttpContext.Request.Path,
1010
StatusCodes.Status500InternalServerError,
1111
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12-
errors)
12+
errors ?? new List<ErrorDetail>())
1313
{
1414
}
15-
public InternalServerErrorDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
15+
public InternalServerErrorDetails(ExceptionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
1616
base(
1717
context ?? throw new ArgumentNullException(nameof(context)),
1818
string.IsNullOrEmpty(title) ? "Internal Server Error" : title,
1919
context.HttpContext.Request.Path,
2020
StatusCodes.Status500InternalServerError,
2121
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22-
errors)
22+
errors ?? new List<ErrorDetail>())
2323
{
2424
}
2525
}

ExceptionAll/Details/NotFoundDetails.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
public class NotFoundDetails : BaseDetails
44
{
5-
public NotFoundDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
5+
public NotFoundDetails(ActionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
66
base(
77
context ?? throw new ArgumentNullException(nameof(context)),
88
string.IsNullOrEmpty(title) ? "Not Found" : title,
99
context.HttpContext.Request.Path,
1010
StatusCodes.Status404NotFound,
1111
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12-
errors)
12+
errors ?? new List<ErrorDetail>())
1313
{
1414
}
15-
public NotFoundDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
15+
public NotFoundDetails(ExceptionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
1616
base(
1717
context ?? throw new ArgumentNullException(nameof(context)),
1818
string.IsNullOrEmpty(title) ? "Not Found" : title,
1919
context.HttpContext.Request.Path,
2020
StatusCodes.Status404NotFound,
2121
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22-
errors)
22+
errors ?? new List<ErrorDetail>())
2323
{
2424
}
2525
}

ExceptionAll/Details/UnauthorizedDetails.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
public class UnauthorizedDetails : BaseDetails
44
{
5-
public UnauthorizedDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
5+
public UnauthorizedDetails(ActionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
66
base(
77
context ?? throw new ArgumentNullException(nameof(context)),
88
string.IsNullOrEmpty(title) ? "Unauthorized" : title,
99
context.HttpContext.Request.Path,
1010
StatusCodes.Status401Unauthorized,
1111
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12-
errors)
12+
errors ?? new List<ErrorDetail>())
1313
{
1414
}
15-
public UnauthorizedDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
15+
public UnauthorizedDetails(ExceptionContext context, string? title = null, string? message = null, List<ErrorDetail>? errors = null) :
1616
base(
1717
context ?? throw new ArgumentNullException(nameof(context)),
1818
string.IsNullOrEmpty(title) ? "Unauthorized" : title,
1919
context.HttpContext.Request.Path,
2020
StatusCodes.Status401Unauthorized,
2121
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22-
errors)
22+
errors ?? new List<ErrorDetail>())
2323
{
2424
}
2525
}

ExceptionAll/ExceptionAll.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
<Authors>Bronson Bata</Authors>
1414
<AssemblyVersion>1.1.0.0</AssemblyVersion>
1515
<FileVersion>1.1.0.0</FileVersion>
16-
<Version>3.0.0</Version>
16+
<Version>3.1.0</Version>
17+
<Nullable>enable</Nullable>
1718
</PropertyGroup>
1819

1920
<ItemGroup>
20-
<PackageReference Include="FluentValidation" Version="10.3.0" />
21+
<PackageReference Include="FluentValidation" Version="10.3.5" />
2122
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
22-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
23-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
24+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
25+
<PackageReference Include="Moq" Version="4.16.1" />
26+
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.2" />
2427
</ItemGroup>
2528

2629
</Project>

ExceptionAll/GlobalUsings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
global using FluentValidation;
99
global using ExceptionAll.Validation;
1010
global using Microsoft.Extensions.Logging;
11-
global using System.Reflection;
11+
global using System.Reflection;
12+
global using Swashbuckle.AspNetCore.Filters;

ExceptionAll/Helpers/ProblemDetailsHelper.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public static class ProblemDetailsHelper
44
{
55
public static void AddDefaultExtensionsFromContext(this BaseDetails details,
66
ActionContext context,
7-
List<ErrorDetail> errors = null)
7+
List<ErrorDetail>? errors = null)
88
{
99
foreach (var (key, value) in GetExtensionsFromContext(context, errors))
1010
{
@@ -14,7 +14,7 @@ public static void AddDefaultExtensionsFromContext(this BaseDetails details,
1414

1515
public static void AddDefaultExtensionsFromContext(this BaseDetails details,
1616
ExceptionContext context,
17-
List<ErrorDetail> errors = null)
17+
List<ErrorDetail>? errors = null)
1818
{
1919
foreach (var (key, value) in GetExtensionsFromContext(context, errors))
2020
{
@@ -23,12 +23,12 @@ public static void AddDefaultExtensionsFromContext(this BaseDetails details,
2323
}
2424

2525
private static IDictionary<string, object> GetExtensionsFromContext(ActionContext context,
26-
List<ErrorDetail> errors = null)
26+
List<ErrorDetail>? errors = null)
2727
{
2828
var dictionary = new Dictionary<string, object>
2929
{
3030
{"Method", context.HttpContext.Request.Method },
31-
{"QueryString", context.HttpContext.Request.QueryString.Value },
31+
{"QueryString", context.HttpContext.Request.QueryString.Value ?? string.Empty },
3232
{"CorrelationId", context.HttpContext.Request.Headers["x-correlation-id"].ToString() },
3333
{"TraceId", context.HttpContext.TraceIdentifier }
3434
};
@@ -39,12 +39,12 @@ private static IDictionary<string, object> GetExtensionsFromContext(ActionContex
3939
}
4040

4141
private static IDictionary<string, object> GetExtensionsFromContext(ExceptionContext context,
42-
List<ErrorDetail> errors = null)
42+
List<ErrorDetail>? errors = null)
4343
{
4444
var dictionary = new Dictionary<string, object>
4545
{
4646
{"Method", context.HttpContext.Request.Method },
47-
{"QueryString", context.HttpContext.Request.QueryString.Value },
47+
{"QueryString", context.HttpContext.Request.QueryString.Value ?? string.Empty },
4848
{"CorrelationId", context.HttpContext.Request.Headers["x-correlation-id"].ToString() },
4949
{"TraceId", context.HttpContext.TraceIdentifier }
5050
};
@@ -54,22 +54,15 @@ private static IDictionary<string, object> GetExtensionsFromContext(ExceptionCon
5454
return dictionary;
5555
}
5656

57-
public static ConstructorInfo GetActionContextConstructor<T>()
57+
public static ConstructorInfo? GetActionContextConstructor<T>()
5858
where T : BaseDetails
5959
{
60-
try
60+
return typeof(T).GetConstructor(new[]
6161
{
62-
return typeof(T).GetConstructor(new[]
63-
{
64-
typeof(ActionContext),
65-
typeof(string),
66-
typeof(string),
67-
typeof(List<ErrorDetail>)
68-
});
69-
}
70-
catch (Exception e)
71-
{
72-
throw new Exception($"Error creating constructor for type: {typeof(T)}", e);
73-
}
62+
typeof(ActionContext),
63+
typeof(string),
64+
typeof(string),
65+
typeof(List<ErrorDetail>)
66+
});
7467
}
7568
}

0 commit comments

Comments
 (0)