自定义API错误代码的最佳实践

huangapple go评论66阅读模式
英文:

Best practices to define custom error codes for an API

问题

我们计划开发一个API。有很多情况可以被视为错误请求。

为此,我们计划发送一个带有错误代码错误信息附加信息的JSON对象。

我在这里放了一个示例,当用户发送一张光照图像时。我们认为这个请求是错误的,并发送以下JSON:

{
   "ErrorCode": 10000,
   "Error": "检测到光照不均匀的图像",
   "Additional Info": "建议更改图像位置"
}
  1. 我们的JSON对象格式是否合适,或者我们需要添加类似于400/404的HTTP错误代码?
  2. 如何选择这个自定义错误代码?我们计划从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"
}

  1. Is our JSON object format is good or we need to add the HTTP Error code like 400/404
  2. 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

huangapple
  • 本文由 发表于 2020年10月5日 15:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64204553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定