Skip to content

错误码设定和返回规范 | Error code setting and return specification #7950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mysterywolf opened this issue Aug 12, 2023 · 2 comments

Comments

@mysterywolf
Copy link
Member

mysterywolf commented Aug 12, 2023

1 RT-Thread 有两种错误码

  • 一种是RT-Thread自己的错误码,如 RT_ERRORRT_EINVAL
  • 另一种是POSIX标准错误码 如 ERROREINVAL

若代码、组件是针对POSIX兼容设计的,则使用POSIX标准错误码,否则使用RT-Thread错误码。

例如:AT组件不是POSIX兼容设计,因此使用RT-Thread错误码;DFSv2是POSIX兼容设计,因此采用POSIX错误码。

2 错误码的正负

  • 以函数返回值方式无论POSIX错误码还是RT-Thread错误码都返回负值错误码 (除了 EOKRT_EOK,这俩不用加负号),例如:
    • return -RT_EINVAL;
    • return -EINVAL;
    • return EOK;
    • return RT_EOK;
  • errno=rt_set_errno方式注入错误码,无论POSIX错误码还是RT-Thread错误码都使用正值错误码, 例如:
    • rt_set_errno(EINVAL);
    • rt_set_errno(RT_EINVAL);
    • errno = EINVAL;
    • errno = RT_EINVAL;
    • rt_set_errno(RT_EOK);
    • errno = EOK;

3 相关

#4791


1 RT-Thread has two error codes

  • One is RT-Thread's own error code, such as RT_ERROR, RT_EINVAL, etc.
  • The other is POSIX standard error codes such as ERROR, EINVAL, etc.

If the code and component are designed for POSIX compatibility, use the POSIX standard error code, otherwise use the RT-Thread error code.

For example: AT components are not POSIX compliant, so RT-Thread error codes are used; DFSv2 is POSIX compliant, so POSIX error codes are used.

2 The sign of the error code

  • Regardless of the POSIX error code or the RT-Thread error code, the function returns a negative value error code (except for EOK and RT_EOK, which do not need to be negative), for example:
    • return -RT_EINVAL;
    • return -EINVAL;
    • return EOK;
    • return RT_EOK;
  • Inject error codes with errno= or rt_set_errno, regardless of POSIX error codes or RT-Thread error codes, use positive error codes, for example:
    • rt_set_errno(EINVAL);
    • rt_set_errno(RT_EINVAL);
    • errno = EINVAL;
    • errno = RT_EINVAL;
    • rt_set_errno(RT_EOK);
    • errno = EOK;
@mysterywolf mysterywolf changed the title 错误码设定和返回规范 错误码设定和返回规范 | Error code setting and return specification Aug 12, 2023
@BernardXiong
Copy link
Member

若代码、组件是针对POSIX兼容设计的,

这个时候RT的错误码会被定义成POSIX的标准错误码

@mysterywolf mysterywolf added the discussion This PR/issue needs to be discussed later label Aug 13, 2023
@mysterywolf
Copy link
Member Author

当POSIX打开之后,rt-thread错误码兼容POSIX错误码

@mysterywolf mysterywolf removed the discussion This PR/issue needs to be discussed later label Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants