ErrorsController

master
Tudor Stanciu 2022-03-04 17:40:43 +02:00
parent 7ec5176555
commit be4ce6cc83
1 changed files with 28 additions and 0 deletions

View File

@ -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<IExceptionHandlerFeature>();
var exception = context.Error;
return exception switch
{
ValidationException => StatusCode(StatusCodes.Status404NotFound, new { exception.Message }),
UnauthorizedAccessException => StatusCode(StatusCodes.Status401Unauthorized, new { exception.Message }),
_ => StatusCode(StatusCodes.Status500InternalServerError),
};
}
}
}