[英]Should I response an HTTP 5xx or 4xx for business logic errors?
If I would need to make some validations on my business logic what status code would you recommend me? 如果我需要对我的业务逻辑进行一些验证,您会向我推荐什么状态代码?
(eg) I have to validate if the username is previously registered. (例如)我必须验证用户名是否先前已注册。
I'd use a 我会用
400 Bad Request 400错误的要求
or 要么
409 Conflict 409冲突
but I'm not really sure if it's the best option. 但我不确定这是否是最佳选择。
As per the HTTP specification: the 5xx
series of response codes should be used in case of "internal server error" - which is to say that something bad happened in the webserver that is the fault of the developer (or the sysadmin - something of that nature) and not the end-user of the webservice. 按照HTTP规范:如果出现“内部服务器错误”,则应使用5xx
系列响应代码-也就是说,开发人员(或sysadmin)的错误是在Web服务器中发生的,性质),而不是网络服务的最终用户。
4xx
means "something is wrong with the data that was sent to the server", for example 404
is for a resource that the client requested that doesn't exist and 400
is for generic "bad request" (eg an XML request was missing a required element, for example). 4xx
表示“发送到服务器的数据有问题”,例如404
表示客户端请求的资源不存在,而400
表示通用的“错误请求”(例如XML请求丢失了例如,必填元素)。
Your example of returning HTTP 409 Conflict
for "username is already registered" seems fine, though strictly speaking this should only be used when the request is on a specific entity. 返回示例“用户名已注册”的HTTP 409 Conflict
示例似乎很好,尽管严格来讲,仅当请求在特定实体上时才应使用。 A 409 response wouldn't be appropriate when the request path does not uniquely identify a specific resource. 当请求路径不能唯一标识特定资源时,409响应将不合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.