Ocelot icon indicating copy to clipboard operation
Ocelot copied to clipboard

Need to catch the exceptions in my custom exceptionMiddleware

Open bwn-z opened this issue 2 years ago • 0 comments

Expected Behavior / New Feature

I want to add my custom exceptionMiddleware and catch all exception in it. I want to do this because I don't like the default error response (statusCodes: 400+, 500+) from ocelot without json body and info.

Obviously you can just add middleware as normal before the call to app.UseOcelot() It cannot be added after as Ocelot does not call the next middleware.

Actual Behavior / Motivation for New Feature

I added my custom exceptionMiddleware and It doesn't get the exceptions after DelegatingHandler callings.

Steps to Reproduce the Problem

  1. Register at Startup.cs an exceptionMiddleware before a registration of Ocelot app.UseMiddleware<ExceptionMiddleware>(); app.UseOcelot().Wait();
  2. Register a some delegating handler at public void ConfigureServices(IServiceCollection services): services.AddOcelot(Configuration).AddDelegatingHandler<SomeDelegatingHandler>();
  3. Use at ocelot.json the configuration: "DelegatingHandlers" : [ "SomeDelegatingHandler" ] for a some route.
  4. ExcetionMiddleware.cs code:
public async Task InvokeAsync(HttpContext context) 
{
    try 
    {
        await _next(context); // <-- set the breakpoint here
    } 
    catch (Exception ex) 
    {
        Console.WriteLine(ex); // <-- set the breakpoint here
    }
}
  1. SomeDelegatingHandler.cs code:
protected override Task < HttpResponseMessage > SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
{
    throw new Exception("test exception"); // <-- set the breakpoint here
    return base.SendAsync(request, cancellationToken);
}
  1. OcelotPipeline catches, handles and writes a log of the error. We can't get them in our exceptionMiddleware. Manipulations with OcelotPipelineConfiguration and its middlewares (PreErrorResponderMiddleware PreAuthenticationMiddleware AuthenticationMiddleware, PreAuthorizationMiddleware, AuthorizationMiddleware, PreQueryStringBuilderMiddleware) with try/catch design don't work too.

Specifications

  • Version: 17.0.1
  • Platform: net5.0
  • Subsystem: Windows 10 x64

bwn-z avatar Apr 29 '22 17:04 bwn-z