From be4ce6cc83e73d261a73fb9d0ad182d589a845c8 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Fri, 4 Mar 2022 17:40:43 +0200 Subject: [PATCH] ErrorsController --- .../Controllers/ErrorsController.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/server/ProxmoxConnector.Server/Controllers/ErrorsController.cs diff --git a/src/server/ProxmoxConnector.Server/Controllers/ErrorsController.cs b/src/server/ProxmoxConnector.Server/Controllers/ErrorsController.cs new file mode 100644 index 0000000..fed03e0 --- /dev/null +++ b/src/server/ProxmoxConnector.Server/Controllers/ErrorsController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.ComponentModel.DataAnnotations; + +namespace ProxmoxConnector.Server.Controllers +{ + [AllowAnonymous] + [ApiExplorerSettings(IgnoreApi = true)] + public class ErrorsController : ControllerBase + { + [Route("error")] + public IActionResult HandleErrors() + { + var context = HttpContext.Features.Get(); + var exception = context.Error; + + return exception switch + { + ValidationException => StatusCode(StatusCodes.Status404NotFound, new { exception.Message }), + UnauthorizedAccessException => StatusCode(StatusCodes.Status401Unauthorized, new { exception.Message }), + _ => StatusCode(StatusCodes.Status500InternalServerError), + }; + } + } +}