Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit 8f8ea0a

Browse files
authored
Merge pull request #24 from AngeloDotNet/6-status-code-management-422
6 status code management 422
2 parents 451a710 + 098aef0 commit 8f8ea0a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ This library is an ad hoc code customization used in my private/work projects th
5353
| 406 | Exception.NotAcceptableException | Response.NotAcceptable | available |
5454
| 408 | Exception.RequestTimeoutException | Response.RequestTimeout | available |
5555
| 409 | Exception.ConflictException | Response.Conflict | available |
56-
| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | coming soon |
56+
| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | available |
5757
| 500 | Exception.InternalServerErrorException | Response.InternalServerError | coming soon |
5858
| 501 | Exception.NotImplementedException | Response.NotImplemented | coming soon |
5959
| 502 | Exception.BadGatewayException | Response.BadGateway | coming soon |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace CustomLibrary.ProblemDetails.Exception;
2+
3+
public class UnprocessableEntityException : System.Exception
4+
{
5+
public UnprocessableEntityException()
6+
{
7+
}
8+
9+
public UnprocessableEntityException(string message) : base(message)
10+
{
11+
}
12+
13+
public UnprocessableEntityException(string message, System.Exception innerException) : base(message, innerException)
14+
{
15+
}
16+
}

src/CustomLibrary.ProblemDetails/Response/Response.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,26 @@ public static ObjectResult Conflict(HttpContext httpContext, System.Exception ex
199199

200200
return result;
201201
}
202+
203+
public static ObjectResult UnprocessableEntity(HttpContext httpContext, System.Exception exc)
204+
{
205+
var statusCode = StatusCodes.Status422UnprocessableEntity;
206+
var problemDetails = new CustomProblemDetails
207+
{
208+
Status = statusCode,
209+
Type = $"https://httpstatuses.com/{statusCode}",
210+
Instance = httpContext.Request.Path,
211+
Title = "UnprocessableEntity"
212+
};
213+
214+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
215+
problemDetails.Extensions.Add("errors", exc.Message);
216+
217+
var result = new ObjectResult(problemDetails)
218+
{
219+
StatusCode = statusCode
220+
};
221+
222+
return result;
223+
}
202224
}

0 commit comments

Comments
 (0)