-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigureExceptionHandlerExtension.cs
More file actions
35 lines (32 loc) · 1.25 KB
/
ConfigureExceptionHandlerExtension.cs
File metadata and controls
35 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Microsoft.AspNetCore.Diagnostics;
using System.Net;
using System.Net.Mime;
using System.Text.Json;
namespace ECommerceAPI.API.Extensions
{
static public class ConfigureExceptionHandlerExtension
{
public static void ConfigureExceptionHandler<T>(this WebApplication application, ILogger<T> logger)
{
application.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = MediaTypeNames.Application.Json;
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError(contextFeature.Error.Message);
await context.Response.WriteAsync(JsonSerializer.Serialize(new
{
StatusCode = context.Response.StatusCode,
Message = contextFeature.Error.Message,
Title = "Error occurred!"
}));
}
});
});
}
}
}