Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!