khellang / Middleware

Various ASP.NET Core middleware
MIT License
813 stars 111 forks source link

How to customise the ProblemDetails object when capturing FluentValidation exceptions? #195

Open iaingymware opened 1 year ago

iaingymware commented 1 year ago

Hi

I have this up and running when using the default ProblemDetails implementation, however I want to update some of the values, for example the type, to link out to my own documentation specifying the validation requirements.

I can't figure out how to override the ProblemDetails template.

I am currently using the example ProblemDetailsOptionsExtensions class from the samp and I can see it grabs the errors and adds them to a dictionary then passes this into a ProblemDetailsFactory class to generate the response.

How can I override the ProblemDetailsFactory implementation to provide my own Type data?

Thanks

khellang commented 1 year ago

Hello @iaingymware! 👋

Have you looked at the sample in this repo? It shows a bunch of ways you can return custom problem details responses. You can return it directly from controllers, use Map functions when configuring the middle ware etc.

It's hard to give more specific feedback without knowing more details about what you want to achieve.

iaingymware commented 1 year ago

Hi @khellang

I am using your example here linked just below to capture the FluentValidation Validation Exception.

https://github.com/khellang/Middleware/blob/master/samples/ProblemDetails.Sample/ProblemDetailsOptionsExtensions.cs

This uses the ProblemsDetailFactory to generate a ProblemsDetails instance with errors. This works brilliantly however I want to customise the returned ProblemDetails instance.

Is there a way to use the sample Extension class however provide specific values, i.e Type, Title, Detail etc or do I need to write my own factory implementation similar to yours?

Thanks

khellang commented 1 year ago

If I'm not mistaken, the factory methods let you specify every single property of the problem details spec.

iaingymware commented 1 year ago

So I could use this, but it doesnt accept errors as a param:

image

Then we have this, but it accepts ModelStateDictionary (which i dont have as this is a web api) and no errors details:

image

This which accepts error and status code, but no other params:

image

and then this which is the same but an IDictionary version rather than SerializableError:

image

Sorry if I've missed something or I am mistaken, but I dont see a way to pass all custom ProblemDetails values, as well as the errors dictionary?

Thanks

iaingymware commented 1 year ago

Hi @khellang

Any update on how I should progress with this?

Thanks