Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eef0bcd

Browse files
san127127Burgyn
authored andcommittedJul 18, 2019
Add BadRequestException for 400 response (#36) (#39)
1 parent 17202a5 commit eef0bcd

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Kros.AspNetCore.Exceptions
6+
{
7+
/// <summary>
8+
/// Exception is thrown when the user request is invalid.
9+
/// </summary>
10+
public class BadRequestException : Exception
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of <see cref="BadRequestException"/> class.
14+
/// </summary>
15+
public BadRequestException()
16+
: this(Properties.Resources.BadRequest)
17+
{
18+
}
19+
20+
/// <summary>
21+
/// Initializes a new instance of <see cref="BadRequestException" /> class.
22+
/// </summary>
23+
/// <param name="message">Message.</param>
24+
public BadRequestException(string message)
25+
: base(message)
26+
{
27+
}
28+
}
29+
}

‎src/Kros.AspNetCore/Middlewares/ErrorHandlingMiddleware.cs

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public async Task Invoke(HttpContext context)
3838
{
3939
await _next.Invoke(context);
4040
}
41+
catch (BadRequestException ex)
42+
{
43+
SetResponseType(context, ex, StatusCodes.Status400BadRequest);
44+
}
4145
catch (UnauthorizedAccessException ex)
4246
{
4347
SetResponseType(context, ex, StatusCodes.Status401Unauthorized);

‎src/Kros.AspNetCore/Properties/Resources.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Kros.AspNetCore/Properties/Resources.resx

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<data name="AuthorizationServiceForbiddenRequest" xml:space="preserve">
121121
<value>Authorization service forbidden this request.</value>
122122
</data>
123+
<data name="BadRequest" xml:space="preserve">
124+
<value>User request is invalid.</value>
125+
</data>
123126
<data name="ErrorHandlingMiddleware_StatusCodeChange" xml:space="preserve">
124127
<value>Exception {0} was thrown during request pipeline. Status code of the response was changed to {1}.</value>
125128
<comment>0 - Full type name of exception.

‎tests/Kros.AspNetCore.Tests/ErrorHandling/ErrorHandlingMiddlewareShould.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ErrorHandlingMiddlewareShould
1515
{
1616
private static readonly (Exception ex, int statusCode)[] _knownExceptions = new (Exception, int)[]
1717
{
18+
(new BadRequestException(), StatusCodes.Status400BadRequest),
1819
(new UnauthorizedAccessException(), StatusCodes.Status401Unauthorized),
1920
(new ResourceIsForbiddenException(), StatusCodes.Status403Forbidden),
2021
(new NotFoundException(), StatusCodes.Status404NotFound),

0 commit comments

Comments
 (0)
Please sign in to comment.