| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System.Net;
-
- namespace BlackRock.Reporting.API.Exceptions
- {
- public class BRExceptionMiddleware
- {
- private readonly RequestDelegate _next;
- private readonly ILoggingBuilder _logger;
- public BRExceptionMiddleware(RequestDelegate next, ILoggingBuilder logger)
- {
- _logger = logger;
- _next = next;
- }
- public async Task InvokeAsync(HttpContext httpContext)
- {
- try
- {
- await _next(httpContext);
- }
- catch (Exception ex)
- {
- //_logger.LogError($"Something went wrong: {ex}");
- await HandleExceptionAsync(httpContext, ex);
- }
- }
- private async Task HandleExceptionAsync(HttpContext context, Exception exception)
- {
- context.Response.ContentType = "application/json";
- context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
- var message = exception switch
- {
- DomainException => "Access violation error from the custom middleware",
- _ => "Internal Server Error from the custom middleware."
- };
- await context.Response.WriteAsync(new Error()
- {
- StatusCode = (HttpStatusCode)context.Response.StatusCode,
- Title = message
- }.ToString());
- }
- }
- }
|