Middleware: Not too sure how to migrate off ExceptionProblemDetails
G’day 👋🏻
I’m migrating to v5 and noticed this warning about ExceptionProblemDetails becoming obsolete.

(note: ignore the code in the image, i just wanted to quickly take a screenie 😊 )
Old:
options.Map<ValidationException>(validationException =>
new ExceptionProblemDetails(validationException, StatusCodes.Status400BadRequest));
New:
options.Map<ValidationException>(validationException =>
new ValidationProblemDetails(validationException.Errors.ToDictionary(key => key.PropertyName, value => new string[] { value.ErrorMessage }))
{
Status = (int)HttpStatusCode.BadRequest
});
I just feel that the updated way I’m doing/trying is … cumbersome?
Would love some thoughts…
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 24 (14 by maintainers)
I’m not sure a class like that would add a lot of value. The bulk of the code for validation errors would still be in the mapping from a validation exception into the dictionary, which you’d still need to write.
Yeah, the problem with
DictionaryKeyPolicyis that it applies to all dictionaries, which could introduce problems with round-tripping. There is an issue for handlingJsonExtensionDataspecifically, but that hasn’t moved anywhere; https://github.com/dotnet/runtime/issues/31167.If you’re using System.Text.Json, it might not camelCase the extensions properly. That might’ve changed since, but I had to do this to make it work properly; https://github.com/khellang/Conference/blob/87b757276dfc7305613f0481449b43c20a25ab12/Conference/ValidationProblemDetails.cs
What JSON serialization are you using? The MW is just using whatever settings you MVC app is using 😊
See https://github.com/khellang/Conference/blob/87b757276dfc7305613f0481449b43c20a25ab12/Conference/Startup.cs#L67 and https://github.com/khellang/Conference/blob/87b757276dfc7305613f0481449b43c20a25ab12/Conference/ValidationProblemDetails.cs 😄