Skip to content

Instantly share code, notes, and snippets.

@romeshniriella
Created June 7, 2021 03:17
Show Gist options
  • Save romeshniriella/a7aac45a733a3cd62c8dd8f166d24d69 to your computer and use it in GitHub Desktop.
Save romeshniriella/a7aac45a733a3cd62c8dd8f166d24d69 to your computer and use it in GitHub Desktop.
a very basic .net core `IExceptionFilter` implementation
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