Question

I am implementing a BufferedMediaTypeFormatter for use in a Web API project, but I can't find any details of how to handle errors when overriding ReadFromStream on BufferedMediaTypeTormatter.

I know that I should be writing errors to the IFormatterLogger.LogError method, but what should I return if the input is invalid? Should I return null? Or throw an exception? If so, which exception should be thrown?

Était-ce utile?

La solution

It depends on the kind of user experience you are looking for.

If you throw an exception, Web API would short-circuit the processing of request and returns a 500 Internal Server response (with or without the exception details based on your HttpConfiguration.IncludeErrorDetailPolicy settings).

If you log errors via the IFormatterLogger.LogError (you can return null in this case), then the request processing is not short-circuited and a user would need to check for the ModelState validity to figure out if there are any errors.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top