Fu Juntang
2021-09-04 658e74cc759db6fb6d51d5287749cff1cfd2a0a2
src/bus_error.cpp
@@ -11,12 +11,21 @@
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",
  "Network fault"
  "Network fault",
  "Send to self error",
  "Receive from wrong end",
  "Service stoped",
  "Exceed resource limit",
  "Service not supported",
  "Resource busy",
  "Resource not provide",
  "Invalid parameters",
  "No enough memory"
};
@@ -42,10 +51,13 @@
char *
bus_strerror(int err)
{
  int s, eindex;
  int s;
  char *buf;
  eindex = err - EBUS_BASE;
  /* Make first caller allocate key for thread-specific data */
  if (err == 0) {
    err = EBUS_BASE;
  }
  s = pthread_once(&once, createKey);
  if (s != 0)
@@ -65,14 +77,31 @@
      err_exit(s, "pthread_setspecific");
  }
  if (eindex < 0 || eindex >= _bus_nerr || _bus_errlist[eindex] == NULL)
  {
    snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", eindex);
  }
  else
  {
    strncpy(buf, _bus_errlist[eindex], 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;