Skip to content

Commit 3c5e557

Browse files
authored
Merge pull request #6 from isaacnborges/customozize-exception-type
Customozize exception type
2 parents 476fc4f + c18b0e0 commit 3c5e557

23 files changed

+353
-96
lines changed

README-nuget.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ throw new NotFoundException("Custom not found exception message");
114114
throw new Exception("Custom exception message");
115115
```
116116

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+
117123
### Samples
118124
Inside the `samples` folder has two projects that could be used for test the and validate the middleware.
119125

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ throw new NotFoundException("Custom not found exception message");
113113
throw new Exception("Custom exception message");
114114
```
115115

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+
116122
### Samples
117123
Inside the `samples` folder has two projects that could be used for test the and validate the middleware.
118124

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.

src/CustomExceptionMiddleware.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using CustomExceptionMiddleware.CustomExceptions;
2+
using CustomExceptionMiddleware.Extensions;
3+
using CustomExceptionMiddleware.Responses;
24
using Microsoft.AspNetCore.Http;
35
using Microsoft.AspNetCore.Http.Features;
46
using Microsoft.Extensions.Logging;
@@ -114,29 +116,19 @@ private object BuildCustomErrorResponse(Exception exception)
114116
{
115117
if (_options.ViewStackTrace)
116118
{
117-
return new CustomErrorDetailResponse
118-
{
119-
Type = GetExceptionType(exception),
120-
Error = new CustomErrorDetail(exception.Message, exception.StackTrace)
121-
};
119+
return new CustomErrorDetailResponse(GetExceptionType(exception), new CustomErrorDetail(exception.Message, exception.StackTrace));
122120
}
123121

124-
return new CustomErrorResponse
125-
{
126-
Type = GetExceptionType(exception),
127-
Error = new CustomError(exception.Message)
128-
};
122+
return new CustomErrorResponse(GetExceptionType(exception), new CustomError(exception.Message));
129123
}
130124

131125
private static string GetExceptionType(Exception exception)
132126
{
133127
var exceptionType = UnexpectedError;
134128

135-
if (exception.GetType().BaseType.Name.Equals(nameof(DomainException)) ||
136-
exception.GetType().Name.Equals(nameof(CannotAccessException)) ||
137-
exception.GetType().Name.Equals(nameof(NotFoundException)))
129+
if (exception is CustomException customException)
138130
{
139-
exceptionType = ValidationErrors;
131+
exceptionType = string.IsNullOrEmpty(customException.ExceptionType) ? ValidationErrors : customException.ExceptionType;
140132
}
141133

142134
return exceptionType;

src/CustomExceptionMiddleware.csproj

Lines changed: 4 additions & 4 deletions
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.6</Version>
5+
<Version>1.1.7</Version>
66
<Authors>Isaac Nunes Borges</Authors>
77
<Company>isaacnborges</Company>
88
<LangVersion>9.0</LangVersion>
@@ -30,9 +30,9 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33-
<None Include="..\icon.png" Pack="true" PackagePath=""/>
34-
<None Include="..\README-nuget.md" Pack="true" PackagePath="\"/>
35-
<None Include="..\LICENSE" Pack="true" PackagePath=""/>
33+
<None Include="..\icon.png" Pack="true" PackagePath="" />
34+
<None Include="..\README-nuget.md" Pack="true" PackagePath="\" />
35+
<None Include="..\LICENSE" Pack="true" PackagePath="" />
3636
</ItemGroup>
3737

3838
</Project>

src/CustomExceptions/CannotAccessException.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace CustomExceptionMiddleware.CustomExceptions
55
{
66
[Serializable]
7-
public class CannotAccessException : Exception
7+
public class CannotAccessException : CustomException
88
{
99
public CannotAccessException()
1010
{ }
@@ -15,6 +15,16 @@ public CannotAccessException(string message) : base(message)
1515
public CannotAccessException(string message, Exception innerException) : base(message, innerException)
1616
{ }
1717

18+
public CannotAccessException(string message, string exceptionType) : base(message)
19+
{
20+
ExceptionType = exceptionType;
21+
}
22+
23+
public CannotAccessException(string message, string exceptionType, Exception innerException) : base(message, innerException)
24+
{
25+
ExceptionType = exceptionType;
26+
}
27+
1828
protected CannotAccessException(SerializationInfo info, StreamingContext context) : base(info, context)
1929
{ }
2030
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace CustomExceptionMiddleware.CustomExceptions
5+
{
6+
[Serializable]
7+
public abstract class CustomException : Exception
8+
{
9+
public string ExceptionType { get; protected set; }
10+
11+
protected CustomException()
12+
{ }
13+
14+
protected CustomException(string message) : base(message)
15+
{ }
16+
17+
protected CustomException(string message, Exception innerException) : base(message, innerException)
18+
{ }
19+
20+
protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context)
21+
{ }
22+
}
23+
}

src/CustomExceptions/DomainException.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace CustomExceptionMiddleware.CustomExceptions
55
{
66
[Serializable]
7-
public abstract class DomainException : Exception
7+
public abstract class DomainException : CustomException
88
{
99
protected DomainException()
1010
{ }
@@ -15,6 +15,16 @@ protected DomainException(string message) : base(message)
1515
protected DomainException(string message, Exception innerException) : base(message, innerException)
1616
{ }
1717

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

src/CustomExceptions/NotFoundException.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace CustomExceptionMiddleware.CustomExceptions
55
{
66
[Serializable]
7-
public class NotFoundException : Exception
7+
public class NotFoundException : CustomException
88
{
99
public NotFoundException()
1010
{ }
@@ -17,5 +17,15 @@ public NotFoundException(string message, Exception innerException) : base(messag
1717

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

src/EnumExtensions.cs renamed to src/Extensions/EnumExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace CustomExceptionMiddleware
3+
namespace CustomExceptionMiddleware.Extensions
44
{
55
public static class EnumExtensions
66
{

0 commit comments

Comments
 (0)