Skip to content

Commit a5e4c76

Browse files
Merge branch 'main' into dotnet-upgrade
# Conflicts: # src/CustomExceptionMiddleware.csproj
2 parents 6615c90 + 3c5e557 commit a5e4c76

22 files changed

+413
-99
lines changed

README-nuget.md

Lines changed: 33 additions & 5 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

@@ -108,8 +114,30 @@ throw new NotFoundException("Custom not found exception message");
108114
throw new Exception("Custom exception message");
109115
```
110116

111-
### Sample example
112-
Open `docs` folder, inside has a [postman](https://www.postman.com/) collection that could be used for test the sample projects with some requests and validate the middleware in use.
117+
#### Customize exception type
118+
It's possible to customize the exception type when throw an exception, just pass the type in an exception constructor.
119+
```c#
120+
throw new CustomDomainException("Custom domain exception message", "OTHER_CUSTOM_TYPE");
121+
```
122+
123+
### Samples
124+
Inside the `samples` folder has two projects that could be used for test the and validate the middleware.
125+
126+
#### Run the sample projects
127+
- WebAppTest
128+
```
129+
dotnet run --project .\samples\CustomExceptionMiddleware.WebAppTest\
130+
```
131+
- WebAppTest.Custom
132+
```
133+
dotnet run --project .\samples\CustomExceptionMiddleware.WebAppTest.Custom\
134+
```
135+
#### Samples documentation
136+
- Swagger
137+
- [WebAppTest](http://localhost:5000/swagger/index.html)
138+
- [WebAppTest.Custom](http://localhost:5001/swagger/index.html)
139+
- Postman
140+
- Open `docs` folder, inside has a [postman](https://www.postman.com/) collection that could be used for test.
113141
114142
## Logging
115143
This middleware will `Log` some informations that can be used for monitoring and observability, like `TraceIdentifier`, request and exception informations like message type and stack trace:

README.md

Lines changed: 34 additions & 7 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

@@ -107,14 +113,35 @@ throw new NotFoundException("Custom not found exception message");
107113
throw new Exception("Custom exception message");
108114
```
109115

110-
### Sample example
111-
Open `docs` folder, inside has a [postman](https://www.postman.com/) collection that could be used for test the sample projects with some requests and validate the middleware in use.
116+
#### Customize exception type
117+
It's possible to customize the exception type when throw an exception, just pass the type in an exception constructor.
118+
```c#
119+
throw new CustomDomainException("Custom domain exception message", "OTHER_CUSTOM_TYPE");
120+
```
121+
122+
### Samples
123+
Inside the `samples` folder has two projects that could be used for test the and validate the middleware.
124+
125+
#### Run the sample projects
126+
- WebAppTest
127+
```
128+
dotnet run --project .\samples\CustomExceptionMiddleware.WebAppTest\
129+
```
130+
- WebAppTest.Custom
131+
```
132+
dotnet run --project .\samples\CustomExceptionMiddleware.WebAppTest.Custom\
133+
```
134+
#### Samples documentation
135+
- Swagger
136+
- [WebAppTest](http://localhost:5000/swagger/index.html)
137+
- [WebAppTest.Custom](http://localhost:5001/swagger/index.html)
138+
- Postman
139+
- Open `docs` folder, inside has a [postman](https://www.postman.com/) collection that could be used for test.
112140
113141
## Logging
114142
This middleware will `Log` some informations that can be used for monitoring and observability, like `TraceIdentifier`, request and exception informations like message type and stack trace:
115143
116144
Example log:
117-
118145
```
119146
Occurred an exception - TraceId: 0HMBO9LGH0JHD:00000002 - ExceptionType: InvalidStateException - Message: Custom domain exception message
120147
CustomExceptionMiddleware.WebAppTest.InvalidStateException: Custom domain exception message

samples/CustomExceptionMiddleware.WebAppTest.Custom/Controllers/ProductController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public IActionResult GetDomain([FromQuery] bool returnProduct = false)
2727
return Ok(result);
2828
}
2929

30+
[HttpGet("custom-domain")]
31+
public IActionResult GetCustomDomain([FromQuery] bool returnProduct = false)
32+
{
33+
var result = _productService.GetCustomDomainException(returnProduct);
34+
return Ok(result);
35+
}
36+
3037
[IgnoreCustomException]
3138
[HttpGet("ignore")]
3239
public IActionResult GetIgnore()

samples/CustomExceptionMiddleware.WebAppTest.Custom/CustomDomainException.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public CustomDomainException(string message) : base(message)
1616
public CustomDomainException(string message, Exception innerException) : base(message, innerException)
1717
{ }
1818

19+
public CustomDomainException(string message, string exceptionType) : base(message, exceptionType)
20+
{
21+
ExceptionType = exceptionType;
22+
}
23+
24+
public CustomDomainException(string message, string exceptionType, Exception innerException) : base(message, exceptionType, innerException)
25+
{
26+
ExceptionType = exceptionType;
27+
}
28+
1929
protected CustomDomainException(SerializationInfo info, StreamingContext context) : base(info, context)
2030
{ }
2131
}

samples/CustomExceptionMiddleware.WebAppTest.Custom/IProductService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public interface IProductService
66
{
77
IEnumerable<Product> Get(int productsCount);
88
IEnumerable<Product> GetDomainException(bool returnProducts);
9+
IEnumerable<Product> GetCustomDomainException(bool returnProducts);
910
}
1011
}

samples/CustomExceptionMiddleware.WebAppTest.Custom/ProductService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public IEnumerable<Product> GetDomainException(bool returnProducts)
1818
throw new CustomDomainException("Custom domain exception message");
1919
}
2020

21+
public IEnumerable<Product> GetCustomDomainException(bool returnProducts)
22+
{
23+
if (returnProducts)
24+
return GetProducts();
25+
26+
throw new CustomDomainException("Custom domain exception message", "OTHER_CUSTOM_TYPE");
27+
}
28+
2129
private static IEnumerable<Product> GetProducts(int customersCount = 10)
2230
{
2331
var fake = new Faker<Product>()

samples/CustomExceptionMiddleware.WebAppTest/InvalidStateException.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public InvalidStateException(string message) : base(message)
1616
public InvalidStateException(string message, Exception innerException) : base(message, innerException)
1717
{ }
1818

19+
public InvalidStateException(string message, string exceptionType) : base(message, exceptionType)
20+
{
21+
ExceptionType = exceptionType;
22+
}
23+
24+
public InvalidStateException(string message, string exceptionType, Exception innerException) : base(message, exceptionType, innerException)
25+
{
26+
ExceptionType = exceptionType;
27+
}
28+
1929
protected InvalidStateException(SerializationInfo info, StreamingContext context) : base(info, context)
2030
{ }
2131
}

src/CustomExceptionMiddlewareExtensions.cs renamed to src/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace CustomExceptionMiddleware
88
/// <summary>
99
/// Extension methods to add custom exception handling to an HTTP application pipeline.
1010
/// </summary>
11-
public static class CustomExceptionMiddlewareExtensions
11+
public static class ApplicationBuilderExtensions
1212
{
1313
/// <summary>
1414
/// Adds the <see cref="CustomExceptionMiddleware"/> to the specified <see cref="IApplicationBuilder"/>, which enables handling exceptions capabilities.

src/CustomErrorDetailResponse.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/CustomErrorResponse.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)