user16276760
user16276760

Reputation:

How does a middleware automatically handle exceptions throw by the middleware below it?

Let's say we have the following code:

public class MiddleWareFirst
{
   private RequestDelegate next;

   public MiddleWareFirst(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      await context.Response.WriteAsync("Start \n");

      if (next != null) {
         await next(context);
      }
   }
}

public class MiddleWareSecond
{
   private RequestDelegate next;

   public MiddleWareSecond(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      if (next != null) {
         await next(context);    // <---- should throw the exception caused by MiddleWareThird
      }
   }
}

public class MiddleWareThird
{
   private RequestDelegate next;

   public MiddleWareThird(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      throw new System.Exception();
   }
}

and build the pipeline as:

// Configure method in startup.cs
app.UseMiddleware<MiddleWareFirst>();
app.UseMiddleware<MiddleWareSecond>();
app.UseMiddleware<MiddleWareThird>();

You can see MiddleWareThird throw an exception. When I run the application, I can see the output of the webpage is "Start", which confuses me, shouldn't it be a 500 error?

Because we know that await can throw the exception of a task. So for MiddleWareSecond, since it doesn't use try catch block to catch the exception, the task that return to MiddleWareFirst should contain the exception internally. Since MiddleWareFirst doesn't use try catch block neither, so the exception will be propagated to the web server which should generate a 500 error page. But the output is normal, so how does the exception thrown in MiddleWareThird magically get swallowed?

Upvotes: 3

Views: 1119

Answers (1)

FireAlkazar
FireAlkazar

Reputation: 1882

exception will be propagated to the web server which should generate a 500 error page

As you have already started response with await context.Response.WriteAsync("Start \n"); status code 200 is written to the response stream as per HTTP protocol you send status in the first line.

If an exception will be thrown before WriteAsync you will get 500 from the server as you expect.

This is handled by aspnet core infrastructure, also in logs you may notice that the exception is logged.

Of course, you can have custom middleware to handle exceptions like DeveloperExceptionPageMiddleware

public async Task Invoke(HttpContext context)
{
    try
    {
        await _next(context);
    }
    catch (Exception ex)
    {
        //...
    }
}

https://github.com/dotnet/aspnetcore/blob/f74c6f12d868b8e5c91e85f1a2bcbd1134ed5fd5/src/Middleware/Diagnostics/src/DeveloperExceptionPage/DeveloperExceptionPageMiddleware.cs#L90-L127

Upvotes: 1

Related Questions