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

Commit 098aef0

Browse files
committed
Implementata exception status code 422 - close #6
1 parent 586a346 commit 098aef0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
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)