galaeth-draft/Galaeth.ApiServer/Middleware/ExceptionMiddleware.cs
2024-11-17 09:31:01 +00:00

108 lines
3.6 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.Net;
using Galaeth.ApiServer.Constants;
using Galaeth.ApiServer.Models.Common;
using Galaeth.Core.Constants;
using Galaeth.Core.Exceptions;
namespace Galaeth.ApiServer.Middleware;
/// <summary>
/// Handle exceptions caught in a request.
/// </summary>
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionMiddleware> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionMiddleware"/> class.
/// </summary>
/// <param name="next">Instance of <see cref="RequestDelegate"/>.</param>
/// <param name="logger">Instance of <see cref="ILogger"/>.</param>
public ExceptionMiddleware(
RequestDelegate next,
ILogger<ExceptionMiddleware> logger)
{
_next = next;
_logger = logger;
}
/// <summary>
/// Invoke middleware.
/// </summary>
/// <param name="httpContext">Instance of <see cref="HttpContext"/>.</param>
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (ServiceException exception)
{
await HandleServiceExceptionAsync(httpContext, exception);
}
catch (ValidationException exception)
{
await HandleValidationExceptionAsync(httpContext, exception);
}
catch (Exception exception)
{
_logger.LogError(
"Unhandled Exception: {path} using {method}: {exception}",
httpContext.Request.Path,
httpContext.Request.Method,
exception);
await HandleOtherExceptionAsync(httpContext);
}
}
/// <summary>
/// Api exception.
/// </summary>
/// <param name="context">Instance of <see cref="HttpContext"/>.</param>
/// <param name="exception">Instance of <see cref="ValidationException"/>.</param>
private static async Task HandleServiceExceptionAsync(HttpContext context, ServiceException exception)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)exception.HttpStatusCode;
await context.Response.WriteAsJsonAsync(new ErrorResultModel
{
Error = exception.ErrorCode,
Message = exception.ErrorMessage,
});
}
/// <summary>
/// Validation failed.
/// </summary>
/// <param name="context">Instance of <see cref="HttpContext"/>.</param>
/// <param name="exception">Instance of <see cref="ValidationException"/>.</param>
private static async Task HandleValidationExceptionAsync(HttpContext context, ValidationException exception)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status422UnprocessableEntity;
await context.Response.WriteAsJsonAsync(new ErrorResultModel
{
Error = CoreErrorCodes.ValidationError,
Message = exception.Message,
});
}
/// <summary>
/// Unexpected exception.
/// </summary>
/// <param name="context">Instance of <see cref="HttpContext"/>.</param>
private static async Task HandleOtherExceptionAsync(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
await context.Response.WriteAsJsonAsync(new ErrorResultModel()
{
Success = false,
Error = ApiErrorCodes.UnknownError,
Message = "Something went wrong. Try again later",
});
}
}