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

Commit 80cd832

Browse files
committed
2 parents aabfc07 + 3ac0c2e commit 80cd832

20 files changed

+378
-424
lines changed

ExceptionAll.APIExample/Controllers/WeatherForecastController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public async Task<IActionResult> GetAll()
4747
public async Task<IActionResult> GetNullRefError(string param)
4848
{
4949
param = null;
50+
await Task.Delay(0);
5051
throw new ArgumentNullException(nameof(param));
5152
}
5253

ExceptionAll.APIExample/ExceptionAll.APIExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

ExceptionAll.Tests/ExceptionAll.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

ExceptionAll.Tests/Services/ActionResultServiceTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Logging;
77
using Moq;
88
using System;
9+
using System.Collections.Generic;
910
using Xunit;
1011
using Xunit.Abstractions;
1112

@@ -49,7 +50,7 @@ public void GetResponse_WithProblemDetails_Throws()
4950
mockErrorResponseService.Object);
5051

5152
// Act
52-
var action = new Func<IActionResult>(() => actionResultService.GetResponse<ProblemDetails>(mockActionContext.Object));
53+
var action = new Func<IActionResult>(() => actionResultService.GetResponse<BaseDetails>(mockActionContext.Object));
5354

5455
// Assert
5556
Assert.Throws<Exception>(action);
@@ -72,8 +73,12 @@ public void GetResponse_WithInvalidInheritedConstructor_Throws()
7273
Assert.Throws<Exception>(action);
7374
}
7475

75-
private class TestDummy : ProblemDetails
76+
private class TestDummy : BaseDetails
7677
{
78+
public TestDummy(ActionContext context, string title, string instance, int? status, string details, List<ErrorDetail> errors) :
79+
base(context, title, instance, status, details, errors)
80+
{
81+
}
7782
}
7883

7984
[Fact]
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
using System;
2-
using ExceptionAll.Dtos;
3-
using ExceptionAll.Helpers;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.AspNetCore.Mvc.Filters;
7-
using System.Collections.Generic;
1+
namespace ExceptionAll.Details;
82

9-
namespace ExceptionAll.Details
3+
public class BadRequestDetails : BaseDetails
104
{
11-
public class BadRequestDetails : ProblemDetails
5+
public BadRequestDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
6+
base(
7+
context ?? throw new ArgumentNullException(nameof(context)),
8+
string.IsNullOrEmpty(title) ? "Bad Request" : title,
9+
context.HttpContext.Request.Path,
10+
StatusCodes.Status400BadRequest,
11+
string.IsNullOrEmpty (message) ? "See errors or logs for more details" : message,
12+
errors)
13+
{
14+
}
15+
public BadRequestDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
16+
base(
17+
context ?? throw new ArgumentNullException(nameof(context)),
18+
string.IsNullOrEmpty(title) ? "Bad Request" : title,
19+
context.HttpContext.Request.Path,
20+
StatusCodes.Status400BadRequest,
21+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22+
errors)
1223
{
13-
public BadRequestDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
14-
{
15-
if (context is null) throw new ArgumentNullException(nameof(context));
16-
Title = string.IsNullOrEmpty(title) == false ? title : "Bad Request";
17-
Instance = context.HttpContext.Request.Path;
18-
Status = StatusCodes.Status400BadRequest;
19-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
20-
this.AddDefaultExtensionsFromContext(context, errors);
21-
}
22-
23-
public BadRequestDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
24-
{
25-
if (context is null) throw new ArgumentNullException(nameof(context));
26-
Title = string.IsNullOrEmpty(title) == false ? title : "Bad Request";
27-
Instance = context.HttpContext.Request.Path;
28-
Status = StatusCodes.Status400BadRequest;
29-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
30-
this.AddDefaultExtensionsFromContext(context, errors);
31-
}
3224
}
33-
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace ExceptionAll.Details;
2+
3+
public class BaseDetails : ProblemDetails
4+
{
5+
public BaseDetails(ActionContext context, string title, string instance, int? status, string details, List<ErrorDetail> errors)
6+
{
7+
if (context is null)
8+
{
9+
throw new ArgumentNullException(nameof(context));
10+
}
11+
12+
Title = title;
13+
Instance = instance;
14+
Status = status;
15+
Detail = details;
16+
this.AddDefaultExtensionsFromContext(context, errors);
17+
}
18+
19+
public BaseDetails(ExceptionContext context, string title, string instance, int? status, string details, List<ErrorDetail> errors)
20+
{
21+
if (context is null)
22+
{
23+
throw new ArgumentNullException(nameof(context));
24+
}
25+
26+
Title = title;
27+
Instance = instance;
28+
Status = status;
29+
Detail = details;
30+
this.AddDefaultExtensionsFromContext(context, errors);
31+
}
32+
}
Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
using System;
2-
using ExceptionAll.Dtos;
3-
using ExceptionAll.Helpers;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.AspNetCore.Mvc.Filters;
7-
using System.Collections.Generic;
1+
namespace ExceptionAll.Details;
82

9-
namespace ExceptionAll.Details
3+
public class ForbiddenDetails : BaseDetails
104
{
11-
public class ForbiddenDetails : ProblemDetails
5+
public ForbiddenDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
6+
base(
7+
context ?? throw new ArgumentNullException(nameof(context)),
8+
string.IsNullOrEmpty(title) ? "Forbidden" : title,
9+
context.HttpContext.Request.Path,
10+
StatusCodes.Status403Forbidden,
11+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12+
errors)
13+
{
14+
}
15+
public ForbiddenDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
16+
base(
17+
context ?? throw new ArgumentNullException(nameof(context)),
18+
string.IsNullOrEmpty(title) ? "Forbidden" : title,
19+
context.HttpContext.Request.Path,
20+
StatusCodes.Status403Forbidden,
21+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22+
errors)
1223
{
13-
public ForbiddenDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
14-
{
15-
if (context is null) throw new ArgumentNullException(nameof(context));
16-
Title = string.IsNullOrEmpty(title) == false ? title : "Forbidden";
17-
Instance = context.HttpContext.Request.Path;
18-
Status = StatusCodes.Status403Forbidden;
19-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
20-
this.AddDefaultExtensionsFromContext(context, errors);
21-
}
22-
23-
public ForbiddenDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
24-
{
25-
if (context is null) throw new ArgumentNullException(nameof(context));
26-
Title = string.IsNullOrEmpty(title) == false ? title : "Forbidden";
27-
Instance = context.HttpContext.Request.Path;
28-
Status = StatusCodes.Status403Forbidden;
29-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
30-
this.AddDefaultExtensionsFromContext(context, errors);
31-
}
3224
}
3325
}
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
using System;
2-
using ExceptionAll.Dtos;
3-
using ExceptionAll.Helpers;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.AspNetCore.Mvc.Filters;
7-
using System.Collections.Generic;
1+
namespace ExceptionAll.Details;
82

9-
namespace ExceptionAll.Details
3+
public class InternalServerErrorDetails : BaseDetails
104
{
11-
public class InternalServerErrorDetails : ProblemDetails
5+
public InternalServerErrorDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
6+
base(
7+
context ?? throw new ArgumentNullException(nameof(context)),
8+
string.IsNullOrEmpty(title) ? "Internal Server Error" : title,
9+
context.HttpContext.Request.Path,
10+
StatusCodes.Status500InternalServerError,
11+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12+
errors)
13+
{
14+
}
15+
public InternalServerErrorDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
16+
base(
17+
context ?? throw new ArgumentNullException(nameof(context)),
18+
string.IsNullOrEmpty(title) ? "Internal Server Error" : title,
19+
context.HttpContext.Request.Path,
20+
StatusCodes.Status500InternalServerError,
21+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22+
errors)
1223
{
13-
public InternalServerErrorDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
14-
{
15-
if (context is null) throw new ArgumentNullException(nameof(context));
16-
Title = string.IsNullOrEmpty(title) == false ? title : "Internal Server Error";
17-
Instance = context.HttpContext.Request.Path;
18-
Status = StatusCodes.Status500InternalServerError;
19-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
20-
this.AddDefaultExtensionsFromContext(context, errors);
21-
}
22-
23-
public InternalServerErrorDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
24-
{
25-
if (context is null) throw new ArgumentNullException(nameof(context));
26-
Title = string.IsNullOrEmpty(title) == false ? title : "Internal Server Error";
27-
Instance = context.HttpContext.Request.Path;
28-
Status = StatusCodes.Status500InternalServerError;
29-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
30-
this.AddDefaultExtensionsFromContext(context, errors);
31-
}
3224
}
33-
}
25+
}
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
using System;
2-
using ExceptionAll.Dtos;
3-
using ExceptionAll.Helpers;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.AspNetCore.Mvc.Filters;
7-
using System.Collections.Generic;
1+
namespace ExceptionAll.Details;
82

9-
namespace ExceptionAll.Details
3+
public class NotFoundDetails : BaseDetails
104
{
11-
public class NotFoundDetails : ProblemDetails
5+
public NotFoundDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
6+
base(
7+
context ?? throw new ArgumentNullException(nameof(context)),
8+
string.IsNullOrEmpty(title) ? "Not Found" : title,
9+
context.HttpContext.Request.Path,
10+
StatusCodes.Status404NotFound,
11+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12+
errors)
13+
{
14+
}
15+
public NotFoundDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
16+
base(
17+
context ?? throw new ArgumentNullException(nameof(context)),
18+
string.IsNullOrEmpty(title) ? "Not Found" : title,
19+
context.HttpContext.Request.Path,
20+
StatusCodes.Status404NotFound,
21+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22+
errors)
1223
{
13-
public NotFoundDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
14-
{
15-
if (context is null) throw new ArgumentNullException(nameof(context));
16-
Title = string.IsNullOrEmpty(title) == false ? title : "Not Found";
17-
Instance = context.HttpContext.Request.Path;
18-
Status = StatusCodes.Status404NotFound;
19-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
20-
this.AddDefaultExtensionsFromContext(context, errors);
21-
}
22-
23-
public NotFoundDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
24-
{
25-
if (context is null) throw new ArgumentNullException(nameof(context));
26-
Title = string.IsNullOrEmpty(title) == false ? title : "Not Found";
27-
Instance = context.HttpContext.Request.Path;
28-
Status = StatusCodes.Status404NotFound;
29-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
30-
this.AddDefaultExtensionsFromContext(context, errors);
31-
}
3224
}
33-
}
25+
}
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
using System;
2-
using ExceptionAll.Dtos;
3-
using ExceptionAll.Helpers;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.AspNetCore.Mvc.Filters;
7-
using System.Collections.Generic;
1+
namespace ExceptionAll.Details;
82

9-
namespace ExceptionAll.Details
3+
public class UnauthorizedDetails : BaseDetails
104
{
11-
public class UnauthorizedDetails : ProblemDetails
5+
public UnauthorizedDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
6+
base(
7+
context ?? throw new ArgumentNullException(nameof(context)),
8+
string.IsNullOrEmpty(title) ? "Unauthorized" : title,
9+
context.HttpContext.Request.Path,
10+
StatusCodes.Status401Unauthorized,
11+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
12+
errors)
13+
{
14+
}
15+
public UnauthorizedDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null) :
16+
base(
17+
context ?? throw new ArgumentNullException(nameof(context)),
18+
string.IsNullOrEmpty(title) ? "Unauthorized" : title,
19+
context.HttpContext.Request.Path,
20+
StatusCodes.Status401Unauthorized,
21+
string.IsNullOrEmpty(message) ? "See errors or logs for more details" : message,
22+
errors)
1223
{
13-
public UnauthorizedDetails(ActionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
14-
{
15-
if (context is null) throw new ArgumentNullException(nameof(context));
16-
Title = string.IsNullOrEmpty(title) == false ? title : "Unauthorized";
17-
Instance = context.HttpContext.Request.Path;
18-
Status = StatusCodes.Status401Unauthorized;
19-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
20-
this.AddDefaultExtensionsFromContext(context, errors);
21-
}
22-
23-
public UnauthorizedDetails(ExceptionContext context, string title = null, string message = null, List<ErrorDetail> errors = null)
24-
{
25-
if (context is null) throw new ArgumentNullException(nameof(context));
26-
Title = string.IsNullOrEmpty(title) == false ? title : "Unauthorized";
27-
Instance = context.HttpContext.Request.Path;
28-
Status = StatusCodes.Status401Unauthorized;
29-
Detail = string.IsNullOrEmpty(message) == false ? message : "See errors or logs for more details";
30-
this.AddDefaultExtensionsFromContext(context, errors);
31-
}
3224
}
33-
}
25+
}

0 commit comments

Comments
 (0)