/
Gerz
/
WebDbfShow
Обзор
Документация
Войти
/
Gerz
/
WebDbfShow
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
WebDBFShow/Extensions/ExceptionMiddlewareExtensions.cs
33 строки
1 KB
usilgtfoms
Add global exception handler
02 фев 2023, 07:06
02 фев 2023, 07:06
98214c0
Код
Авторство
О чём код?
using Entities.Models.ErrorModel; using Microsoft.AspNetCore.Diagnostics; using System.Net; namespace WebDBFShow.Extensions { public static class ExceptionMiddlewareExtensions { public static void ConfigureExceptionHandler(this IApplicationBuilder app, ILogger logger) { app.UseExceptionHandler(appError => { appError.Run(async context => { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.ContentType = "application/json"; var contextFeature = context.Features.Get<IExceptionHandlerFeature>(); if (contextFeature != null) { logger.LogError($"Something went wrong: {contextFeature.Error}"); await context.Response.WriteAsync(new ErrorDetails() { StatusCode = context.Response.StatusCode, Message = "Internal Server Error." }.ToString()); } }); }); } } }