Created
June 7, 2021 03:17
-
-
Save romeshniriella/a7aac45a733a3cd62c8dd8f166d24d69 to your computer and use it in GitHub Desktop.
a very basic .net core `IExceptionFilter` implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExceptionFilter : IExceptionFilter | |
{ | |
private readonly ILogger<ExceptionFilter> _logger; | |
public ExceptionFilter(ILogger<ExceptionFilter> logger) | |
{ | |
_logger = logger; | |
} | |
public void OnException(ExceptionContext context) | |
{ | |
_logger.LogError(context.Exception, "An unexpected error was encountered"); | |
context.Result = new ObjectResult( | |
new ErrorResponse | |
{ | |
ErrorMessage = $"An unexpected error was encountered: {context.Exception.Message}", | |
Name = context.Exception.Source, | |
RequestId = context.HttpContext?.TraceIdentifier | |
}) | |
{ | |
StatusCode = (int)HttpStatusCode.InternalServerError | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment