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), + }; + } + } +}