ob_error.h 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef OBERROR_ERROR_H
#define OBERROR_ERROR_H

#include "share/ob_errno.h"
#include "os_errno.h"
#include <iostream>
#include <cstdio>
using namespace oceanbase::common;

// code error print define
#define ERROR_PRINT(fmt, ...) printf("%s[%d]-<%s>: " #fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)

enum Fac {
  ORA,  // oracle mode // ora
  PLS,  // oracle mode // pls
  MY,   // mysql mode
  NONE
};

struct OSErrorInfo {
public:
  OSErrorInfo() : error_name_(nullptr), error_msg_(nullptr), error_code_(-1)
  {}
  virtual ~OSErrorInfo()
  {}
  void free_space()
  {
    if (nullptr != error_name_) {
      free(error_name_);
      error_name_ = nullptr;
    }
    if (nullptr != error_msg_) {
      free(error_msg_);
      error_msg_ = nullptr;
    }
  }

  char* error_name_;
  char* error_msg_;
  int error_code_;
};

struct MySQLErrorInfo {
public:
  MySQLErrorInfo()
      : error_name_(nullptr),
        error_msg_(nullptr),
        mysql_errno_(-1),
        sqlstate_(nullptr),
        cause_(nullptr),
        solution_(nullptr),
        ob_error_(-1)
  {}
  virtual ~MySQLErrorInfo()
  {}
  void free_space()
  {
    if (nullptr != error_name_) {
      free(error_name_);
      error_name_ = nullptr;
    }
    if (nullptr != error_msg_) {
      free(error_msg_);
      error_msg_ = nullptr;
    }
    if (nullptr != sqlstate_) {
      free(sqlstate_);
      sqlstate_ = nullptr;
    }
    if (nullptr != cause_) {
      free(cause_);
      cause_ = nullptr;
    }
    if (nullptr != solution_) {
      free(solution_);
      solution_ = nullptr;
    }
  }

  char* error_name_;
  char* error_msg_;
  int mysql_errno_;
  char* sqlstate_;
  char* cause_;
  char* solution_;
  int ob_error_;
};

struct OracleErrorInfo {
public:
  OracleErrorInfo()
      : error_name_(nullptr),
        error_msg_(nullptr),
        cause_(nullptr),
        solution_(nullptr),
        facility_(NONE),
        error_code_(-1),
        ob_error_(-1)
  {}
  virtual ~OracleErrorInfo()
  {}
  void free_space()
  {
    if (nullptr != error_name_) {
      free(error_name_);
      error_name_ = nullptr;
    }
    if (nullptr != error_msg_) {
      free(error_msg_);
      error_msg_ = nullptr;
    }
    if (nullptr != cause_) {
      free(cause_);
      cause_ = nullptr;
    }
    if (nullptr != solution_) {
      free(solution_);
      solution_ = nullptr;
    }
  }

  char* error_name_;
  char* error_msg_;
  char* cause_;
  char* solution_;
  Fac facility_;
  int error_code_;
  int ob_error_;
};

struct OBErrorInfo {
public:
  OBErrorInfo() : error_name_(nullptr), error_msg_(nullptr), cause_(nullptr), solution_(nullptr), error_code_(-1)
  {}
  virtual ~OBErrorInfo()
  {}
  void free_space()
  {
    if (nullptr != error_name_) {
      free(error_name_);
      error_name_ = nullptr;
    }
    if (nullptr != error_msg_) {
      free(error_msg_);
      error_msg_ = nullptr;
    }
    if (nullptr != cause_) {
      free(cause_);
      cause_ = nullptr;
    }
    if (nullptr != solution_) {
      free(solution_);
      solution_ = nullptr;
    }
  }

  char* error_name_;
  char* error_msg_;
  char* cause_;
  char* solution_;
  int error_code_;
};

// The length of the second dimension, in order to solve the conflict of multiple identical error codes
constexpr int OB_MAX_SAME_ERROR_COUNT = 10;
constexpr int ORACLE_SPECIAL_ERROR_CODE = 600;
constexpr int ORACLE_MAX_ERROR_CODE = 65535;
constexpr int ORACLE_MSG_PREFIX = 11;  // strlen("ORA-00000: ")
constexpr float OB_ERROR_VERSION = 1.0;

static const char* facility_str[NONE] = {"ORA", "PLS", "MY"};

class ObErrorInfoMgr {
public:
  ObErrorInfoMgr();
  virtual ~ObErrorInfoMgr();

  bool insert_os_error(const char* name, const char* msg, int error_code);
  void print_os_error();
  bool is_os_error_exist()
  {
    return 0 < os_error_count_;
  }

  bool insert_mysql_error(const char* name, const char* msg, int mysql_errno, const char* sqlstate, const char* cause,
      const char* solution, int ob_error);
  void print_mysql_error();
  bool is_mysql_error_exist()
  {
    return 0 < mysql_error_count_;
  }

  bool insert_oracle_error(const char* name, const char* msg, const char* cause, const char* solution, Fac facility,
      int error_code, int ob_error);
  void print_oracle_error();
  bool is_oracle_error_exist()
  {
    return 0 < oracle_error_count_;
  }

  bool insert_ob_error(const char* name, const char* msg, const char* cause, const char* solution, int error_code);
  void print_ob_error();
  bool is_ob_error_exist()
  {
    return 0 < ob_error_count_;
  }

private:
  int os_error_count_;
  OSErrorInfo os_error_[OS_MAX_SAME_ERROR_COUNT];
  int mysql_error_count_;
  MySQLErrorInfo mysql_error_[OB_MAX_SAME_ERROR_COUNT];
  int oracle_error_count_;
  OracleErrorInfo oracle_error_[OB_MAX_SAME_ERROR_COUNT];
  int ob_error_count_;
  OBErrorInfo ob_error_[OB_MAX_SAME_ERROR_COUNT];
};

// oracle error code -> ob error map // Maximum number of identical error codes is OB_MAX_SAME_ERROR_COUNT
static int g_oracle_ora[ORACLE_MAX_ERROR_CODE][OB_MAX_SAME_ERROR_COUNT];
static int g_oracle_pls[ORACLE_MAX_ERROR_CODE][OB_MAX_SAME_ERROR_COUNT];
// mysql error code -> ob error map // Maximum number of identical error codes is OB_MAX_SAME_ERROR_COUNT
static int g_mysql_error[OB_MAX_ERROR_CODE][OB_MAX_SAME_ERROR_COUNT];
// os error code -> ob error map // Maximum number of identical error codes is OS_MAX_SAME_ERROR_COUNT
static int g_os_error[OS_MAX_ERROR_CODE][OS_MAX_SAME_ERROR_COUNT];

// adder
bool add_os_info(int error_code, ObErrorInfoMgr* mgr);
bool add_ob_info(int error_code, ObErrorInfoMgr* mgr);
bool add_oracle_info(Fac oracle_facility, int error_code, int argument, ObErrorInfoMgr* mgr);
bool add_mysql_info(int error_code, ObErrorInfoMgr* mgr);

// parser
void parse_error_code(const char* argv, int& error_code);
void parse_facility(const char* argv, Fac& facility);
bool parse_param(int args, char* argv[]);

// printer
bool print_error_info(Fac facility, int error_code, int argument);

// init
bool init_global_info();

#endif /* OBERROR_ERROR_H */