Home  >  Q&A  >  body text

javascript - How to design the return code of the project so that different return codes represent different meanings?

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

高洛峰高洛峰2686 days ago737

reply all(4)I'll reply

  • 黄舟

    黄舟2017-06-12 09:21:41

    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.

    reply
    0
  • 迷茫

    迷茫2017-06-12 09:21:41

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-06-12 09:21:41

    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.

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-12 09:21:41

    $con = array('code'=>4000,'data'=>'result');
    echo json_encode($con);

    reply
    0
  • Cancelreply