Skip to content

Instantly share code, notes, and snippets.

@Regenhardt
Created April 21, 2021 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Regenhardt/08831aeee4136e47d4351b7ca89ff29a to your computer and use it in GitHub Desktop.
Save Regenhardt/08831aeee4136e47d4351b7ca89ff29a to your computer and use it in GitHub Desktop.
public class ExceptionHandler : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
try
{
await next(context);
}
catch (Exception e)
{
var result = JsonSerializer.Serialize(Result.Negative(e.ToString()));
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment