diff --git a/README.md b/README.md index 0258907..661c61b 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ This library is an ad hoc code customization used in my private/work projects th | 406 | Exception.NotAcceptableException | Response.NotAcceptable | available | | 408 | Exception.RequestTimeoutException | Response.RequestTimeout | available | | 409 | Exception.ConflictException | Response.Conflict | available | -| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | coming soon | +| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | available | | 500 | Exception.InternalServerErrorException | Response.InternalServerError | coming soon | | 501 | Exception.NotImplementedException | Response.NotImplemented | coming soon | | 502 | Exception.BadGatewayException | Response.BadGateway | coming soon | diff --git a/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs b/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs new file mode 100644 index 0000000..1262168 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class UnprocessableEntityException : System.Exception +{ + public UnprocessableEntityException() + { + } + + public UnprocessableEntityException(string message) : base(message) + { + } + + public UnprocessableEntityException(string message, System.Exception innerException) : base(message, innerException) + { + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails/Response/Response.cs b/src/CustomLibrary.ProblemDetails/Response/Response.cs index edae28d..3af2ad7 100644 --- a/src/CustomLibrary.ProblemDetails/Response/Response.cs +++ b/src/CustomLibrary.ProblemDetails/Response/Response.cs @@ -199,4 +199,26 @@ public static ObjectResult Conflict(HttpContext httpContext, System.Exception ex return result; } + + public static ObjectResult UnprocessableEntity(HttpContext httpContext, System.Exception exc) + { + var statusCode = StatusCodes.Status422UnprocessableEntity; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "UnprocessableEntity" + }; + + problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier); + problemDetails.Extensions.Add("errors", exc.Message); + + var result = new ObjectResult(problemDetails) + { + StatusCode = statusCode + }; + + return result; + } } \ No newline at end of file