added exception handle middleware

master
Young 6 months ago
parent ca651e76a6
commit e796a4c0b2

@ -1,12 +1,14 @@
using Asp.Versioning.ApiExplorer;
using Infrastructure.Seed;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Serilog;
namespace Infrastructure.Extensions;
public static class ApplicationBuilderExtension
{
public static void UseDefaultSwaggerUI(this IApplicationBuilder app)
public static IApplicationBuilder UseDefaultSwaggerUI(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
var apiVersionDescriptionProvider =
@ -19,5 +21,26 @@ public static class ApplicationBuilderExtension
description.GroupName.ToUpperInvariant());
}
});
return app;
}
public static IApplicationBuilder UseDefaultExceptionHandler(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
app.UseExceptionHandler(options =>
{
options.Run(async context=>
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status200OK;
var exceptionHandlerPathFeature =
context.Features.Get<IExceptionHandlerPathFeature>();
var message = new MessageData(false, "unknown error", 500);
Log.Logger.Error(exceptionHandlerPathFeature?.Error.Message!);
await context.Response.WriteAsync(message.Serialize());
});
});
return app;
}
}
Loading…
Cancel
Save