Ocelot
Ocelot copied to clipboard
Need to catch the exceptions in my custom exceptionMiddleware
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.
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
- Register at Startup.cs an exceptionMiddleware before a registration of Ocelot
app.UseMiddleware<ExceptionMiddleware>()
;app.UseOcelot().Wait();
- Register a some delegating handler at
public void ConfigureServices(IServiceCollection services)
:services.AddOcelot(Configuration).AddDelegatingHandler<SomeDelegatingHandler>();
- Use at ocelot.json the configuration:
"DelegatingHandlers" : [ "SomeDelegatingHandler" ]
for a some route. - 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
}
}
- 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);
}
- 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