如何处理REST异常? [英] How to handle REST Exceptions?
问题描述
我们正在进行有关如何处理REST异常的讨论.
We are in the middle of a ongoing discussion about how to handle REST exceptions.
响应内容类型:JSON
Response Content type : JSON
我们有两种解决方案:
- 将所有未检查的异常作为JSON响应扔出去.
- 发送请求无效的响应代码.
参数:
- 当出现错误时,为什么返回JSON?只需发送无效的响应码即可.
计数器参数:
- 响应代码对于普通开发人员来说太技术化了.
你怎么说?
推荐答案
对于我最近开发的JSON API,我同时做到了.我总是使用有效的JSON进行响应(假设我完全响应).如果检测到无效请求,则使用状态400.如果检测到服务器错误(我不认为是由于无效请求引起的),则使用5xx状态. JSON对象包含一个仅针对错误设置的特殊键,具有字符串值.
For a JSON API I recently developed I do both. I always respond with valid JSON (well, assuming I respond at all). If I detect an invalid request, I use status 400. If I detect a server error (which I don't believe is caused by an invalid request), I use a 5xx status. The JSON object contains a special key that is only set for errors, with a string value.
我认为这是一个尊重REST原则的好解决方案,并且可以以多种方式使用.某些其他JSON API(例如Yahoo Search)也使用相同的解决方案.尝试 http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid = YahooDemo& output = json .
I think this is a good solution that respects REST principles, and can be used in multiple ways. The same solution is used by some other JSON APIs, such as Yahoo Search. Try http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&output=json .
这篇关于如何处理REST异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!