exception.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. /**
  21. * @file exception.h
  22. *
  23. * This file contains a declaration of the Exception class and helper register macro
  24. */
  25. #ifndef CXXUTIL_EXCEPTION_H_
  26. #define CXXUTIL_EXCEPTION_H_
  27. #include <stdexcept>
  28. #include <string>
  29. namespace edk {
  30. /**
  31. * @brief edk base exception class, derived from exception.
  32. */
  33. class Exception : public std::exception {
  34. public:
  35. /**
  36. * @brief Error code enumaration
  37. */
  38. enum Code {
  39. INTERNAL = 0, ///< internal error
  40. UNSUPPORTED = 1, ///< unsupported function
  41. INVALID_ARG = 2, ///< invalid argument
  42. MEMORY = 3, ///< memory error
  43. TIMEOUT = 4, ///< timeout
  44. INIT_FAILED = 5, ///< create failed or init failed
  45. UNAVAILABLE = 6, ///< resoure unavailable
  46. };
  47. /**
  48. * @brief Get error code as string
  49. *
  50. * @return error code as string
  51. */
  52. std::string CodeString() const noexcept {
  53. #define RETURN_CODE_STRING(code) \
  54. case code: \
  55. return #code
  56. switch (code_) {
  57. RETURN_CODE_STRING(INTERNAL);
  58. RETURN_CODE_STRING(UNSUPPORTED);
  59. RETURN_CODE_STRING(INVALID_ARG);
  60. RETURN_CODE_STRING(MEMORY);
  61. RETURN_CODE_STRING(TIMEOUT);
  62. default:
  63. return "UNKNOWN";
  64. }
  65. #undef RETURN_CODE_STRING
  66. }
  67. /**
  68. * @brief Constructor.
  69. *
  70. * @param code error code
  71. * @param file source file name where the error has occurred
  72. * @param line line number in the source file where the error has occurred
  73. * @param func function name.
  74. * @param msg error description
  75. */
  76. explicit Exception(Code code, const std::string& file, int line, const std::string& func, const std::string& msg)
  77. : code_(code) {
  78. msg_ = file.substr(file.find_last_of('/') + 1) + ":" + std::to_string(line) + " (" + func + ") " + CodeString() +
  79. "] " + msg;
  80. }
  81. /**
  82. * @brief Get error code
  83. *
  84. * @return error code
  85. */
  86. Code ErrorCode() const noexcept { return code_; }
  87. /**
  88. * @brief Get formatted error message
  89. *
  90. * @return formatted error message
  91. */
  92. const char* what() const noexcept override { return msg_.c_str(); }
  93. private:
  94. Code code_;
  95. std::string msg_;
  96. }; // class Exception
  97. } // namespace edk
  98. /**
  99. * @def THROW_EXCEPTION(CNAME)
  100. * @brief Throw exception with status code and description
  101. * @param code status code
  102. * @param msg exception description
  103. * @see edk::Exception
  104. */
  105. #define THROW_EXCEPTION(code, msg) \
  106. do { \
  107. throw edk::Exception(code, __FILE__, __LINE__, __func__, msg); \
  108. } while (0)
  109. #endif // CXXUTIL_EXCEPTION_H_