第三节 错误状态(Error Status)

第三节 错误状态(Error Status)

Most of libapr functions return apr_status_t value. apr_status_t value is each of APR_SUCCESS or the others. APR_SUCCESS just indicates success. A typical code looks as follows:

大多数APR函数返回一个apr_status_t类型的值,apr_status_t包含一个APR_SUCCESS,APR_SUCCESS表示这个函数执行成功,一个典型的代码如下:

/* pseudo code about apr_status_t check */
apr_status_t rv;
rv = apr_pool_create(&mp, NULL);
if (rv != APR_SUCCESS) {
   ERROR-HANDLING;
}

libapr defines some error statuses such as APR_EINVAL, and some error-check macros such as APR_STATUS_IS_ENOMEM((). Some of them are rather useful on handling portability issues. A typical one is APR_STATUS_IS_EAGAIN() macro. Historically, there are two error numbers that have the same meaning, EAGAIN and EAGAIN. APR_STATUS_IS_EAGAIN() macro takes care of the issue.

APR中定义一些错误状态(如APR_EINVAL)和一些错误检查的宏(如APR_STATUS_IS_ENOMEM),他们中的一些在处理一些小问题上是相当有用的,一个典型的宏是APR_STATUS_IS_EAGAIN,历史遗留原因有两个错误代码表示相同的含义他们是EAGAIN和EAGAIN。APR_STATUS_IS_EAGAIN宏用于处理这个问题。

Nevertheless, it is almost impossible to deal with all error cases independently from many OSes. libapr doesn't reinvent the wheel on error handling. All libapr does is very simple.

  • In success, return APR_SUCCESS
  • In libapr layer's error, return APR_FOO error code
  • In very common OS error, return APR_FOO error code
  • In most of OS dependent error, return OS error number with the offset

然而,处理全部的独立于各种操作系统的错误几乎是不可能的。APR没有重新创建一种错误处理模式,全部的APR错误处理都是非常简单的。

  • 成功时返回APR_SUCCESS
  • 在APR的错误返回APR_FOO错误代码
  • 在非常不通操作系统错误返回APR_FOO的代码
  • 在大多数依赖于操作系统的错误,返回使用offset作为OS的错误代码。

I recommend you to follow the simple rules.

  • To compare return value with APR_SUCCESS
  • If you need to know more error details, to compare the values with the other error code

我推荐你遵循这样的规则:

  • 只需要比较返回值是否为APR_SUCCESS,来获取函数是否运行成功
  • 如果你需要知道错误的具体细节的时候,需要使用其他的错误代码表示

One API that you had better know is apr_strerror(). You can show the error description as follows:

你最好了解一个API他就是apr_strerror(),你可以是用这个API获得这个错误的具体表述信息,示例如下:

/* pseudo code about apr_strerror() */
apr_status_t rv;
rv = apr_foo_bar();
if (rv != APR_SUCCESS) {
   char errbuf[256];
   apr_strerror(rv, buf, sizeof(buf));
   puts(errbuf);  /* show the error description */
}