Skip to content

Commit 0ce3128

Browse files
update return of exception type, now only the map exceptions will return "VALIDATION_ERRORS", all others will return "UNEXPECTED_ERROR"
1 parent 35b62bb commit 0ce3128

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

README-nuget.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ In both cases the output will include de stack trace in `detail` object property
6767
Example output
6868
```json
6969
{
70+
"type": "VALIDATION_ERRORS",
7071
"error": {
7172
"msg": "Custom domain exception message",
7273
"detail": "at CustomExceptionMiddleware.WebAppTest.Custom.ProductService.GetDomainException(Boolean returnProducts) in C:\\isaacnborges\\projects\\custom-exception-middleware\\tests\\CustomExceptionMiddleware.WebAppTest.Custom\\ProductService.cs:line 18\r\n at CustomExceptionMiddleware.WebAppTest.Custom.Controllers.ProductController.GetDomain(Boolean returnProduct) in C:\\isaacnborges\\projects\\custom-exception-middleware\\tests\\CustomExceptionMiddleware.WebAppTest.Custom\\Controllers\\ProductController.cs:line 26"
7374
},
74-
"type": "VALIDATION_ERRORS"
7575
}
7676
```
7777

@@ -95,8 +95,14 @@ Exception InternalServerError 500
9595
```c#
9696
public class InvalidStateException : DomainException
9797
{
98-
public InvalidStateException(string message) : base(message)
99-
{ }
98+
public InvalidStateException()
99+
{ }
100+
101+
public InvalidStateException(string message) : base(message)
102+
{ }
103+
104+
public InvalidStateException(string message, Exception innerException) : base(message, innerException)
105+
{ }
100106
}
101107
```
102108

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ In both cases the output will include de stack trace in `detail` object property
6868
Example output
6969
```json
7070
{
71+
"type": "VALIDATION_ERRORS",
7172
"error": {
7273
"msg": "Custom domain exception message",
7374
"detail": "at CustomExceptionMiddleware.WebAppTest.Custom.ProductService.GetDomainException(Boolean returnProducts) in C:\\isaacnborges\\projects\\custom-exception-middleware\\tests\\CustomExceptionMiddleware.WebAppTest.Custom\\ProductService.cs:line 18\r\n at CustomExceptionMiddleware.WebAppTest.Custom.Controllers.ProductController.GetDomain(Boolean returnProduct) in C:\\isaacnborges\\projects\\custom-exception-middleware\\tests\\CustomExceptionMiddleware.WebAppTest.Custom\\Controllers\\ProductController.cs:line 26"
74-
},
75-
"type": "VALIDATION_ERRORS"
75+
}
7676
}
7777
```
7878

@@ -94,8 +94,14 @@ The custom middleware supports the following **Exceptions**:
9494
```c#
9595
public class InvalidStateException : DomainException
9696
{
97-
public InvalidStateException(string message) : base(message)
98-
{ }
97+
public InvalidStateException()
98+
{ }
99+
100+
public InvalidStateException(string message) : base(message)
101+
{ }
102+
103+
public InvalidStateException(string message, Exception innerException) : base(message, innerException)
104+
{ }
99105
}
100106
```
101107

src/CustomExceptionMiddleware.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,32 @@ private object BuildCustomErrorResponse(Exception exception)
116116
{
117117
return new CustomErrorDetailResponse
118118
{
119-
Type = exception.GetType().Name.Equals(nameof(Exception)) ? UnexpectedError : ValidationErrors,
119+
Type = GetExceptionType(exception),
120120
Error = new CustomErrorDetail(exception.Message, exception.StackTrace)
121121
};
122122
}
123123

124124
return new CustomErrorResponse
125125
{
126-
Type = exception.GetType().Name.Equals(nameof(Exception)) ? UnexpectedError : ValidationErrors,
126+
Type = GetExceptionType(exception),
127127
Error = new CustomError(exception.Message)
128128
};
129129
}
130130

131+
private static string GetExceptionType(Exception exception)
132+
{
133+
var exceptionType = UnexpectedError;
134+
135+
if (exception.GetType().BaseType.Name.Equals(nameof(DomainException)) ||
136+
exception.GetType().Name.Equals(nameof(CannotAccessException)) ||
137+
exception.GetType().Name.Equals(nameof(NotFoundException)))
138+
{
139+
exceptionType = ValidationErrors;
140+
}
141+
142+
return exceptionType;
143+
}
144+
131145
private static void ConfigureResponseContext(HttpContext httpContext, HttpStatusCode statusCode)
132146
{
133147
httpContext.Response.ContentType = "application/json";

src/CustomExceptionMiddleware.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0; net5.0</TargetFrameworks>
5-
<Version>1.1.5</Version>
5+
<Version>1.1.6</Version>
66
<Authors>Isaac Nunes Borges</Authors>
77
<Company>isaacnborges</Company>
88
<LangVersion>9.0</LangVersion>

0 commit comments

Comments
 (0)