Skip to content

Instantly share code, notes, and snippets.

@BashkaMen
Created October 31, 2021 19:24
Show Gist options
  • Save BashkaMen/1644f204ae39463eed36ef036b86b6ee to your computer and use it in GitHub Desktop.
Save BashkaMen/1644f204ae39463eed36ef036b86b6ee to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Udr.Application;
namespace Udr.Web
{
public class ExceptionHandler : IMiddleware
{
private readonly ILogger<ExceptionHandler> _logger;
public ExceptionHandler(ILogger<ExceptionHandler> logger)
{
_logger = logger;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var message = "unknown error";
try
{
await next(context);
return;
}
catch (AppError e)
{
_logger.LogWarning(e, "App error");
message = e.Message;
}
catch (Exception e)
{
_logger.LogError(e, "Error while process request");
}
context.Response.StatusCode = 500;
await context.Response.WriteAsJsonAsync(new { message});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment