question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ProblemDetails middleware when Response has started

See original GitHub issue

Hi @khellang

I have one scenario that I would like to support with ProblemDetails. I usually create a unit of work middleware to make request transactional and consistent:

public class UnitOfWorkMiddleware
{
    private readonly RequestDelegate next;

    public UnitOfWorkMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        await next(context);
        var requestMethod = context.Request.Method;
        var isSafeMethod = requestMethod == HttpMethods.Get || requestMethod == HttpMethods.Head;
        var dbContext = context.RequestServices.GetService(typeof(EFDbContext)) as EFDbContext;
        if (IsSuccessStatusCode(context.Response) && !isSafeMethod)
        {
            await dbContext.SaveChangesAsync();
        }
    }

    private bool IsSuccessStatusCode(HttpResponse response)
    {
        return response.StatusCode >= 200 && response.StatusCode <= 299;
    }
}

In case that SaveChanges() fail, problems details detect the exception but the response has started and rethrow the exception, but in this case I would like to continue using Problem Details.

One question related with this line https://github.com/khellang/Middleware/blob/4ff1f88d86573be0545ee40f60f01362568742b6/src/ProblemDetails/ProblemDetailsMiddleware.cs#L84

Why check if response has been started and not catch all exceptions? Can you tell me some scenario when you have to re throw the exception and not send a problems details to the client?

Actually I don’t know to manage my scenario in the right way.

Regards!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Sep 7, 2018

Hmm, yeah, the problem seems to be that the DbContext has been disposed by the time the OnStarting callback is invoked.

Do you really need this to be custom middleware? I think it would be much easier if this was part of the MVC filter pipeline instead. That would let you save your changes before the result is written to the response body, without callback gymnastics.

1reaction
khellangcommented, Sep 7, 2018

Why check if response has been started and not catch all exceptions?

Because the whole point of the middleware is to send an RFC7807 response to the client. If the response has already started, it’s too late for the middleware to do anything. That’s why it logs and rethrows the exception, in case anyone else is interested in it, like some other error handler or logging middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the ProblemDetails middleware in ASP.NET Core
ProblemDetails is a machine-readable format that is used to standardize error messages in API controllers and represent HTTP API responses based ...
Read more >
Handling Web API Exceptions with ProblemDetails ...
In this short post I describe a handy error-handling middleware, created by Kristian Hellang, that is used to return ProblemDetails results ...
Read more >
Using the ProblemDetails Class in ASP.NET Core Web API
ProblemDetails class in ASP.NET Core Web APIs helps us to standardize our error handling and have better communication with API clients.
Read more >
Handle errors in ASP.NET Core web APIs
SuppressMapClientErrors prevents a ProblemDetails response from being created, even when calling WriteAsync for an API Controller endpoint.
Read more >
Exception handling using Hellang middleware in .Net Core ...
Here, I use a custom exception with extra fields that are needed for problem details object in response, and I use the Instance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found