Add Exception middleware
This commit is contained in:
22
Shared/AppException.cs
Normal file
22
Shared/AppException.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Diary.Shared
|
||||
{
|
||||
public class AppException : Exception
|
||||
{
|
||||
public string Title { get; }
|
||||
public HttpStatusCode StatusCode { get; }
|
||||
|
||||
public AppException(HttpStatusCode statusCode, string title, string message) : base(message)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Title = title;
|
||||
}
|
||||
|
||||
public AppException(HttpStatusCode statusCode, string title, string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Title = title;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Shared/ExceptionDetails.cs
Normal file
31
Shared/ExceptionDetails.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Diary.Shared
|
||||
{
|
||||
public class ExceptionDetails
|
||||
{
|
||||
public string? Message { get; set; }
|
||||
public string? Title { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var jsonSerializerSettings = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
return JsonSerializer.Serialize(this, jsonSerializerSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidationExceptionDetails : ExceptionDetails
|
||||
{
|
||||
public List<ValidationProblemDescriptor> ModelState { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ValidationProblemDescriptor
|
||||
{
|
||||
public string? Property { get; set; }
|
||||
public string[]? Errors { get; set; }
|
||||
}
|
||||
}
|
||||
60
Shared/ExceptionHandlingMiddleware.cs
Normal file
60
Shared/ExceptionHandlingMiddleware.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
namespace Diary.Shared
|
||||
{
|
||||
public partial class ExceptionHandlingMiddleware : IMiddleware
|
||||
{
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
|
||||
public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
||||
{
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error received from ExceptionHandlingMiddleware");
|
||||
await HandleExceptionAsync(context, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task HandleExceptionAsync(HttpContext httpContext, Exception exception)
|
||||
{
|
||||
ExceptionDetails exceptionDetails;
|
||||
httpContext.Response.ContentType = "application/json";
|
||||
|
||||
if (exception.InnerException is AppException)
|
||||
{
|
||||
exception = exception.InnerException;
|
||||
}
|
||||
|
||||
if (exception is AppException applicationException)
|
||||
{
|
||||
httpContext.Response.StatusCode = (int)applicationException.StatusCode;
|
||||
|
||||
exceptionDetails = new()
|
||||
{
|
||||
Message = applicationException.Message,
|
||||
Title = applicationException.Title
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
httpContext.Response.StatusCode = 500;
|
||||
|
||||
exceptionDetails = new()
|
||||
{
|
||||
Message = exception.Message,
|
||||
Title = "API Error"
|
||||
};
|
||||
}
|
||||
|
||||
await httpContext.Response.WriteAsync(exceptionDetails.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user