-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalExceptionFilter.cs
35 lines (29 loc) · 1.26 KB
/
GlobalExceptionFilter.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
namespace Exception.Lib;
public class GlobalExceptionFilter : IExceptionFilter
{
private readonly ILoggerAdapter<GlobalExceptionFilter> _logger;
public GlobalExceptionFilter(ILoggerAdapter<GlobalExceptionFilter> logger)
{
_logger = logger;
}
public void OnException(ExceptionContext context)
{
var exception = context.Exception;
context.ExceptionHandled = true;
_logger.LogError(exception.ToString(), exception.Message.ToString());
switch (exception)
{
case EntityNotFoundException notFoundValidation:
context.Result = new NotFoundObjectResult(new { IsSuccess = false, ErrorCode = notFoundValidation.ErrorCode, Message = notFoundValidation.Message });
context.ExceptionHandled = true;
break;
case BaseException baseException:
context.Result = new BadRequestObjectResult(new { IsSuccess = false, ErrorCode = baseException.ErrorCode, Message = baseException.Message });
context.ExceptionHandled = true;
break;
default:
context.Result = new BadRequestObjectResult(new { IsSuccess = false, ErrorCode = ErrorCodes.UNKNOWN });
break;
}
}
}