wangzhengquan
2021-03-01 98d6460918ee7918fa591e12ed0698b14e539c9d
src/bus_error.cpp
@@ -11,11 +11,16 @@
static pthread_key_t strerrorKey;
static char *_bus_errlist[_bus_nerr] = {
static const char *_bus_errlist[_bus_nerr] = {
  "\0",
  "timeout",
  "Timed out",
  "The other end is not inline",
  "Key already in use"
  "Key already in use",
  "Network fault",
  "Send to self error",
  "Receive from wrong end",
  "Service stoped",
  "Exceed resource limit"
};
@@ -43,7 +48,6 @@
{
  int s;
  char *buf;
  /* Make first caller allocate key for thread-specific data */
  s = pthread_once(&once, createKey);
@@ -64,14 +68,31 @@
      err_exit(s, "pthread_setspecific");
  }
  if (err < 0 || err >= _bus_nerr || _bus_errlist[err] == NULL)
  {
    snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
  }
  else
  {
    strncpy(buf, _bus_errlist[err], MAX_ERROR_LEN - 1);
    buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
  if(err < EBUS_BASE) {
    // libc错误
    if (err < 0 || err >= _sys_nerr || _sys_errlist[err] == NULL)
    {
      snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
    }
    else
    {
      strncpy(buf, _sys_errlist[err], MAX_ERROR_LEN - 1);
      buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
    }
  } else {
    //自定义错误
    err -= EBUS_BASE;
    if (err < 0 || err >= _bus_nerr || _bus_errlist[err] == NULL)
    {
      snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
    }
    else
    {
      strncpy(buf, _bus_errlist[err], MAX_ERROR_LEN - 1);
      buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
    }
  }
  return buf;