error.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef MYSQLX_ERROR_H
  31. #define MYSQLX_ERROR_H
  32. /**
  33. @file
  34. Classes used to access query and command execution results.
  35. */
  36. #include "common.h"
  37. #include "detail/error.h"
  38. #include <memory>
  39. namespace mysqlx {
  40. MYSQLX_ABI_BEGIN(2,0)
  41. /**
  42. An error, warning or other diagnostic information reported by server
  43. when executing queries or statements. The information can be printed to
  44. output stream using `<<` operator.
  45. @note Normally, errors reported by server are not represented by `Warning`
  46. instances but instead they are thrown as instances of `mysqlx::Error`.
  47. @ingroup devapi
  48. */
  49. class Warning
  50. : public virtual common::Printable
  51. , internal::Warning_detail
  52. {
  53. public:
  54. /// Type of diagnostic information.
  55. enum Level {
  56. LEVEL_ERROR, ///< %Error
  57. LEVEL_WARNING, ///< %Warning
  58. LEVEL_INFO ///< Other information
  59. };
  60. private:
  61. Warning(Level level, uint16_t code, const string &msg)
  62. : Warning_detail(byte(level), code, msg)
  63. {
  64. }
  65. Warning(Warning_detail &&init)
  66. : Warning_detail(std::move(init))
  67. {}
  68. void print(std::ostream &out) const
  69. {
  70. try {
  71. Warning_detail::print(out);
  72. }
  73. CATCH_AND_WRAP
  74. }
  75. public:
  76. /**
  77. Return level of the diagnostic info stored in this object.
  78. */
  79. Level getLevel() const
  80. {
  81. return Level(m_level);
  82. }
  83. /**
  84. Return error/warning code reported by server.
  85. */
  86. uint16_t getCode() const
  87. {
  88. return m_code;
  89. }
  90. /**
  91. Return diagnostic message reported by server.
  92. */
  93. const string& getMessage() const
  94. {
  95. return m_msg;
  96. }
  97. ///@cond IGNORE
  98. friend internal::Result_detail;
  99. ///@endcond
  100. struct Access;
  101. friend Access;
  102. };
  103. inline
  104. void internal::Warning_detail::print(std::ostream &out) const
  105. {
  106. switch (Warning::Level(m_level))
  107. {
  108. case Warning::LEVEL_ERROR: out << "Error"; break;
  109. case Warning::LEVEL_WARNING: out << "Warning"; break;
  110. case Warning::LEVEL_INFO: out << "Info"; break;
  111. default: out << "<Unknown>"; break;
  112. }
  113. if (m_code)
  114. out << " " << m_code;
  115. out << ": " << m_msg;
  116. }
  117. MYSQLX_ABI_END(2,0)
  118. } // mysqlx
  119. #endif