简体   繁体   English

来自 API 的错误代码......处理的好习惯是什么

[英]Error codes from API … What is the good practice to handle

I am writing a host application using a proprietary API.我正在使用专有的 API 编写主机应用程序。 This API returns error codes when something wrong happened.当发生错误时,此 API 返回错误代码。 I need your advise(s) about the best way to push up the error codes and manage them in my host application: Here an short example:我需要您就在我的主机应用程序中推送错误代码和管理它们的最佳方法提供建议:这里有一个简短的示例:

CCas.cpp CCas.cpp

CMD_STATUS CCas::shutdown() const
{
        /* CMD_STATUS_OK is an API return code */
    uint8_t statusByte = CMD_STATUS_OK;

    /* Shutdown the CAS system */
    if (CMD_STATUS_OK != (statusByte = CAS_Master_ShutDown()))
        return statusByte;

    /* Set nReset to HIGH */
    ...
        ...

    /* Done the system is OFF */
    return statusByte;
}

main.cpp主文件

int main(char *argc, char **argv)
{
  CMD_STATUS retByte = CMD_STATUS_OK;
  CCas vmx;

  if(retByte != vmx.shutdown())
  /* Something wrong happened */
  return retByte;

  return 0
}

In my example, inside the method shutdown when an error is raised I push up to the main the error inside the statusByte variable and inside the main I catch the error and stop the program.在我的示例中,在引发错误时关闭方法内部,我将 statusByte 变量内部的错误和 main 内部的错误推送到 main 中,我捕获错误并停止程序。

Do I use it in the right way or there is another way to do.我是否以正确的方式使用它,或者还有其他方法可以做。 Do I need to create my own error codes in the main.我是否需要在 main.js 中创建自己的错误代码?

Please could you advise.请你指教。

Thanks谢谢

There is no definite answer here, and there is no single approach that is the ultimate golden approach to take.这里没有明确的答案,也没有一种方法是最终的黄金方法。
Whatever you do, I would say - keep it consistent.无论你做什么,我都会说 - 保持一致。
If you are writing C++ code, you should probably consider using exceptions instead of error codes.如果您正在编写 C++ 代码,您可能应该考虑使用异常而不是错误代码。
Exceptions help writing your host code in a carefree way and help breaking error handling logic from the main code:异常有助于以无忧无虑的方式编写主机代码,并有助于打破主代码中的错误处理逻辑:

try {
    apiCall();
    anotherApiCall();
    yetAnotherApiCall();
} catch (someKindOfError){
    // Handle error here
} catch (anotherKindOfError){
    // Handle other errors here
} catch (baseError){
    // A more generic error handling here
} catch (...){
    // Just make sure you don't catch what you can't handle and leave higher layers handle it
}

Another approach would be to use error codes, the code is ravaged with if this then that but it's a valid approach non the less.另一种方法是使用错误代码, if this then that ,代码就会被破坏,但它是一种有效的方法。
In this case, I would form a constant that says success (like the posix 0) and then something like:在这种情况下,我将形成一个表示成功的常量(如 posix 0),然后是:

ErrorType retVal = someApiCall();
if (retVal != ErrorCodeSuccess){
    // either handle generic error or try to find out if it's a specific error, etc.
    if (retVal == ErrorCodeCatIsHungry){
        // Handle error here and probably bail out by calling 'return retVal;'
    }
}

Some people use the approach of returning a failed code ( void functions returning booleans, functions that return objects return null or a static flag object to denote an error) and then call a more detailed error function on request: getLastErrorOfMyLovelyApi() . Some people use the approach of returning a failed code ( void functions returning booleans, functions that return objects return null or a static flag object to denote an error) and then call a more detailed error function on request: getLastErrorOfMyLovelyApi() .
Not my personal taste, but sometimes useful in C APIs where the error can be a complex set of information.不是我个人的口味,但有时在 C API 中有用,其中错误可能是一组复杂的信息。
It all comes down to who your audience is, what tools you have for them (C++ has exceptions, C does not), and your personal taste.这一切都取决于你的受众是谁,你为他们准备了什么工具(C++ 有例外,C 没有),以及你的个人品味。
If you ask me, exceptions (even just the standard ones from stdexcept ) is the way to go in your case.如果您问我,在您的情况下,异常(甚至只是stdexcept中的标准异常)是通往 go 的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM