英文:
Best practices to define custom error codes for an API
问题
我们计划开发一个API。有很多情况可以被视为错误请求。
为此,我们计划发送一个带有错误代码
、错误信息
和附加信息
的JSON对象。
我在这里放了一个示例,当用户发送一张光照图像时。我们认为这个请求是错误的,并发送以下JSON:
{
"ErrorCode": 10000,
"Error": "检测到光照不均匀的图像",
"Additional Info": "建议更改图像位置"
}
- 我们的JSON对象格式是否合适,或者我们需要添加类似于400/404的HTTP错误代码?
- 如何选择这个自定义错误代码?我们计划从10000开始分配错误代码数字。
可以有人帮助我们或者提供一些值得信赖的资源吗?
英文:
We are planning to develop an API. There are so many cases that a request can be considered as a bad request.
For this, We have planned to send a JSON object with error code
, error
,
and additional info
I have put the sample case here when a user sends an illumination image. We considered as this request is bad request and send the following JSON
{
"ErrorCode": 10000,
"Error": "Detected uneven illumination image",
"Additional Info": "Prefer to change the image location"
}
- Is our JSON object format is good or we need to add the HTTP Error code like 400/404
- How to select this custom error code? We are planning to take the error code number from 10000.
Can someone please help us or provide some trustful resources?
答案1
得分: 4
Check Stripe API they also use error codes (code
) to explain different 4xx
errors.
英文:
Check Stripe API they also use error codes (code
) to explain different 4xx
errors
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论