在 REST 服务上返回错误消息的最佳方式? [英] Best way to return error messages on REST services?

查看:37
本文介绍了在 REST 服务上返回错误消息的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 REST API 的示例,例如 Netflix http://developer.netflix.com/docs/REST_API_Reference#0_59705 和 Twitter,他们似乎将错误消息放在 statusText 标头响应而不是 responseText 中.我们正在开发一个内部 RESTful api,我主张发送自定义 statusText 消息并忽略 responseText.

I've been looking at examples of REST API's like Netflix http://developer.netflix.com/docs/REST_API_Reference#0_59705 and Twitter and they seem to place error messages in the statusText header response instead of the responseText. We're developing an internal RESTful api and I am arguing for sending custom statusText messages and ignoring the responseText.

对于我们的应用范围,当用户尝试做一些他们不应该做的事情时,我们将返回错误 400,并且将在用户界面中为用户更新的唯一错误消息将传递 400. 我认为应该将消息作为修改后的 statusText 发送,但其中一位工程师(他对 REST 的了解比我少一点)主张在 responseText 中发送它.

For the scope of our app, we're returning error 400 when the user has tried doing something they aren't supposed to, and the only error messages that will be updated in the UI for the user will be delivered with 400. I am of the belief that the message should be sent as a modified statusText but one of the engineers (who knows a bit less about REST than me) is arguing for sending it in the responseText.

最好的方法是什么?

推荐答案

我认为你是对的,一般的方法是使用 HTTP 内置的现有错误机制.

I think you're right, the general approach is use the existing error mechanism built into HTTP.

通常,尝试将您的错误映射到现有的 HTTP 错误,例如,如果他们请求他们没有权限的内容,则返回 403 错误.

In general, try to map your errors to existing HTTP errors, for example if they request something they don't have permission to, return a 403 error.

如果他们请求的东西不存在,返回 404.

If they request something that doesn't exist, return a 404.

  • 亚历克斯

这篇关于在 REST 服务上返回错误消息的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆