Skip to content

ExceptionHandler is rethrowing exceptions when mapping an exception to status code 404 #26528

Closed
@danikun

Description

@danikun

Describe the bug

I had a lambda exception handler in my middleware pipeline to handle some custom exceptions and map them to particular status codes in the response. Since I updated from 3.1 to 5.0rc1 when I map the exception to 404, the exception is rethrown after being handled.

I have mitigated the issue with Response.CompleteAsync(), and it works when the app is running normally, but the exception is still thrown and this breaks my integration tests.

To Reproduce

The easiest way to reproduce is adding a lambda exception handler in this form:

            app.UseExceptionHandler(errorApp =>
            {
                errorApp.Run(async context =>
                {
                    var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    
                    await context.Response.WriteAsJsonAsync(new { Message = "Not found" });
                    await context.Response.CompleteAsync();
                });
            });

And create a controller throwing an exception, call this controller and see that the exception is rethrown after being handled.

Further technical details

  • ASP.NET Core version: 5.0rc1
  • Using VS 16.8.0 Preview 3.2

Metadata

Metadata

Assignees

Labels

area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions