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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top