javascript - How to design the return code of the project so that different return codes represent different meanings?
高洛峰
高洛峰 2017-06-12 09:19:41
0
4
771

This is the return code of the WeChat public platform interface. When working on a project, you need the API interface return code. I want to know how to design the API interface return code?


What I mean is how to design different return codes to represent different meanings. For example, 40001 means XXX means, 40002 means XXX means. Should these be designed like this? Thank you everyone

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
黄舟

In application development, it is better to use error names than error codes, and the code is more readable.

Imagine your client code:

if (error === 'ERROR_INVALID_ACCESS_TOKEN') { ... }

vs

if (error === 40010) { ... }

Domestic API providers sometimes use numeric error codes to take care that domestic developers are not familiar with English (or the API developers themselves do not remember English names).

Digital codes are of greater significance at the system level because of high transmission efficiency (such as binary protocols). For a large number of JSON format string transmission protocols, this efficiency is not important.

Readability is much more important, and error names have the advantage of being unstructured and scalable compared to error codes.

迷茫

Give you my thoughts,
Special return code: (common to all interfaces)
0, represents success
-1, represents internal server error
Common error return code:
The first number represents return to different interfaces,
The rest are error types, ordered in descending order of importance.

In fact, there is no need to study it in such depth. I think this is enough.
It’s similar to WeChat.
The important thing is to keep it simple.

小葫芦

You can learn from Microsoft, for example, ERROR_SUCCESS is 0, which means success. Error codes are defined starting at 1 and going up to over 10,000. Then you can divide the error codes into some intervals, such as 1~1000, 1001~2000, 2001~3000, etc., to determine what type of meaning they represent respectively. Then each interval can be defined starting from the basic error codes that can be thought of, and then It can also be amplified in small amounts.

漂亮男人
$con = array('code'=>4000,'data'=>'result');
echo json_encode($con);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template