简体   繁体   English

是否应该针对业务逻辑错误响应HTTP 5xx或4xx?

[英]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.

相关问题 当4xx响应失去连接时,发出多个HTTP请求/响应 - Make multiple HTTP requests/responses when 4xx response loses the connection 使用RavenDB时,我应该在哪里放置业务逻辑 - Where should I place business logic when using RavenDB 如何选择xx-xxx-xx-x格式的ID? - How can i select some id with xx-xxx-xx-x format? 我的业务逻辑应该去哪里? - Where should my Business Logic go? 所有业务逻辑都应该在域模型中吗? - Should all business logic be in domain models or? 类型XX存在于两个DLL中 - The Type XX exists in both DLLs Android应用程序无法正常工作:“ xx.xx应用程序(xx.xx进程)意外停止。请重试 - Android application does not works: "The xx.xx application (xx.xx process) stopped unexpectedly. Try again 我们应该在业务逻辑层(BLL)中拥有什么 - What should we have in a Business Logic Layer (BLL) 业务逻辑应放在控制器/模型/视图项目中的何处 - Where should business logic be placed in a controller/model/view project 我是否应该将业务域合并到一个业务层 - Should I unite business domains into one business layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM