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.

[Docs] Purpose of ProblemDetails library

See original GitHub issue

I’ve seen various mentions of Hellang.Middleware.ProblemDetails and I’m trying to figure out what it does. Regrettably, the readme and sample code don’t explain.

I suspect it catches unhandled exceptions and converts them to ProblemDetails. However that’s something that can be done in an exception middleware/filter/handler/whatever. Since this library seems really “fancy”, I’m sure it does more than just that! 😃

Can someone please tell me what this library actually “does”? Thanks!

(BTW: I’m not trolling, I really want to know. The Microsoft docs site even recommends this library.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
khellangcommented, Jan 26, 2022

Yeah, I think that matches what I would expect 👍

Regarding the 422 status code; it’s an opinion of mine that syntactically correct, but semantically invalid requests should return back a different status code than 400 Bad Request:

From RFC 7231:

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

It’s increasingly common to use 422 Unprocessable Entity for this 😃

From RFC 4918:

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

Some people don’t like it (often because it’s part of the WebDAV RFC and not an “official” HTTP RFC (but this will soon change, with the inclusion of 422 in HTTPbis’ upcoming HTTP Semantics RFC, which obsoletes RFC 7231), so I’ve added an option to change it:

https://github.com/khellang/Middleware/blob/eb7f59cb5aaff0727466622af6230db12703a32f/src/ProblemDetails/ProblemDetailsOptions.cs#L133-L136

3reactions
lonix1commented, Jan 26, 2022

Some more info about AddProblemDetailsConventions, based on khellang’s comments here:

  • It swaps MVC’s built-in ProblemDetailsFactory to use the middleware’s configuration instead. This will give you more consistent errors, no matter where they are produced. I.e. from MVC’s invalid state filter etc.
  • It turns off MVC’s built-in client error mapping, which also gives you more consistent error responses
  • It tries to set the produced error response type on API controllers
  • It adds a filter that makes returning pre-baked ProblemDetails instances from controllers a bit more consistent, by calling the middleware hooks
Read more comments on GitHub >

github_iconTop Results From Across the Web

ProblemDetails Class (Microsoft.AspNetCore.Mvc)
A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of...
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 >
Problem Details in .Net 7 - Meysam Hadeli's Blog
Problem Detail provide machine-readable details of errors in a HTTP response to avoid the need to define new error response formats for HTTP ......
Read more >
ProblemDetail (Spring Framework 6.0.11 API)
Representation for an RFC 7807 problem detail. Includes spec-defined properties, and a properties map for additional, non-standard properties.
Read more >
RFC 7807 - Problem Details for HTTP APIs
The data model for problem details is a JSON [RFC7159] object; when formatted as a JSON document, it uses the "application/problem+json" media type....
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