From 32293e77d2700824c1674ab778017b26226e42e5 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Thu, 17 Aug 2023 00:03:07 +0200 Subject: [PATCH 1/2] Aggiornato README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a49437..6c1bd6d 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ This library is an ad hoc code customization used in my private/work projects th | 400 | Exception.BadRequestException | Response.BadRequest | available | | 401 | Exception.UnauthorizedException | Response.Unauthorized | available | | 403 | Exception.ForbiddenException | Response.Forbidden | available | -| 404 | Exception.NotFoundException | Response.NotFound | coming soon | +| 404 | Exception.NotFoundException | Response.NotFound | available | | 405 | Exception.NotAllowedException | Response.MethodNotAllowed | coming soon | | 406 | Exception.NotAcceptableException | Response.NotAcceptable | coming soon | | 408 | Exception.RequestTimeoutException | Response.RequestTimeout | coming soon | From 1e6c3e0c0cb0a5ccc79cd7c30e9033b44bf3f0a5 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Thu, 17 Aug 2023 00:03:48 +0200 Subject: [PATCH 2/2] Implementata exception status code 404 closes #13 --- .../Exception/NotFoundException.cs | 16 ++++++++++++++ .../Response/Response.cs | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/NotFoundException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/NotFoundException.cs b/src/CustomLibrary.ProblemDetails/Exception/NotFoundException.cs new file mode 100644 index 0000000..66d00bf --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/NotFoundException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class NotFoundException : System.Exception +{ + public NotFoundException() + { + } + + public NotFoundException(string message) : base(message) + { + } + + public NotFoundException(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 f722a12..faf3b65 100644 --- a/src/CustomLibrary.ProblemDetails/Response/Response.cs +++ b/src/CustomLibrary.ProblemDetails/Response/Response.cs @@ -89,4 +89,26 @@ public static ObjectResult Forbidden(HttpContext httpContext, System.Exception e return result; } + + public static ObjectResult NotFound(HttpContext httpContext, System.Exception exc) + { + var statusCode = StatusCodes.Status404NotFound; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "NotFound" + }; + + 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