-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionFilter.cs
39 lines (35 loc) · 1.18 KB
/
ExceptionFilter.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
using BasicWebApi.Models;
namespace BasicWebApi.Filters
{
public class ExceptionFilter : IEndpointFilter
{
private readonly ILogger _logger;
private readonly bool _hideExceptionDetailsToClients;
public ExceptionFilter(ILoggerFactory loggerFactory, IConfiguration configuration)
{
_logger = loggerFactory.CreateLogger<ExceptionFilter>();
_hideExceptionDetailsToClients = configuration.GetValue<bool>("HideExceptionDetailsToClients");
}
public virtual async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
object? result = null;
try
{
result = await next(context);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
if (_hideExceptionDetailsToClients)
{
return new ErrorResult<string>
{
Messages = new[] { "An error occurred." }
};
}
throw;
}
return result;
}
}
}