| Name | Message | Date |
|---|---|---|
| 📄 ExceptionMiddleware.cs | 4 days ago |
📄
ExceptionMiddleware.cs
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
36
37
38
39
40
41
42
using System.Net; using System.Text.Json; using Walley.Checkout.Api.Models; namespace Walley.Checkout.Api.Middleware; public class ExceptionMiddleware { private readonly RequestDelegate _next; private readonly ILogger<ExceptionMiddleware> _logger; public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger) { _next = next; _logger = logger; } public async Task InvokeAsync(HttpContext context) { try { await _next(context); } catch (Exception ex) { _logger.LogError(ex, "An unhandled exception occurred"); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var response = new ApiErrorResponse { Message = "An unexpected error occurred.", StatusCode = context.Response.StatusCode }; var json = JsonSerializer.Serialize(response); await context.Response.WriteAsync(json); } } }