Created
October 31, 2021 19:24
-
-
Save BashkaMen/1644f204ae39463eed36ef036b86b6ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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