exceptions.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include "yaml-cpp/mark.h"
  9. #include "yaml-cpp/traits.h"
  10. #include <sstream>
  11. #include <stdexcept>
  12. #include <string>
  13. // This is here for compatibility with older versions of Visual Studio
  14. // which don't support noexcept
  15. #ifdef _MSC_VER
  16. #define YAML_CPP_NOEXCEPT _NOEXCEPT
  17. #else
  18. #define YAML_CPP_NOEXCEPT noexcept
  19. #endif
  20. namespace YAML {
  21. // error messages
  22. namespace ErrorMsg {
  23. const char* const YAML_DIRECTIVE_ARGS =
  24. "YAML directives must have exactly one argument";
  25. const char* const YAML_VERSION = "bad YAML version: ";
  26. const char* const YAML_MAJOR_VERSION = "YAML major version too large";
  27. const char* const REPEATED_YAML_DIRECTIVE = "repeated YAML directive";
  28. const char* const TAG_DIRECTIVE_ARGS =
  29. "TAG directives must have exactly two arguments";
  30. const char* const REPEATED_TAG_DIRECTIVE = "repeated TAG directive";
  31. const char* const CHAR_IN_TAG_HANDLE =
  32. "illegal character found while scanning tag handle";
  33. const char* const TAG_WITH_NO_SUFFIX = "tag handle with no suffix";
  34. const char* const END_OF_VERBATIM_TAG = "end of verbatim tag not found";
  35. const char* const END_OF_MAP = "end of map not found";
  36. const char* const END_OF_MAP_FLOW = "end of map flow not found";
  37. const char* const END_OF_SEQ = "end of sequence not found";
  38. const char* const END_OF_SEQ_FLOW = "end of sequence flow not found";
  39. const char* const MULTIPLE_TAGS =
  40. "cannot assign multiple tags to the same node";
  41. const char* const MULTIPLE_ANCHORS =
  42. "cannot assign multiple anchors to the same node";
  43. const char* const MULTIPLE_ALIASES =
  44. "cannot assign multiple aliases to the same node";
  45. const char* const ALIAS_CONTENT =
  46. "aliases can't have any content, *including* tags";
  47. const char* const INVALID_HEX = "bad character found while scanning hex number";
  48. const char* const INVALID_UNICODE = "invalid unicode: ";
  49. const char* const INVALID_ESCAPE = "unknown escape character: ";
  50. const char* const UNKNOWN_TOKEN = "unknown token";
  51. const char* const DOC_IN_SCALAR = "illegal document indicator in scalar";
  52. const char* const EOF_IN_SCALAR = "illegal EOF in scalar";
  53. const char* const CHAR_IN_SCALAR = "illegal character in scalar";
  54. const char* const TAB_IN_INDENTATION =
  55. "illegal tab when looking for indentation";
  56. const char* const FLOW_END = "illegal flow end";
  57. const char* const BLOCK_ENTRY = "illegal block entry";
  58. const char* const MAP_KEY = "illegal map key";
  59. const char* const MAP_VALUE = "illegal map value";
  60. const char* const ALIAS_NOT_FOUND = "alias not found after *";
  61. const char* const ANCHOR_NOT_FOUND = "anchor not found after &";
  62. const char* const CHAR_IN_ALIAS =
  63. "illegal character found while scanning alias";
  64. const char* const CHAR_IN_ANCHOR =
  65. "illegal character found while scanning anchor";
  66. const char* const ZERO_INDENT_IN_BLOCK =
  67. "cannot set zero indentation for a block scalar";
  68. const char* const CHAR_IN_BLOCK = "unexpected character in block scalar";
  69. const char* const AMBIGUOUS_ANCHOR =
  70. "cannot assign the same alias to multiple nodes";
  71. const char* const UNKNOWN_ANCHOR = "the referenced anchor is not defined";
  72. const char* const INVALID_NODE =
  73. "invalid node; this may result from using a map iterator as a sequence "
  74. "iterator, or vice-versa";
  75. const char* const INVALID_SCALAR = "invalid scalar";
  76. const char* const KEY_NOT_FOUND = "key not found";
  77. const char* const BAD_CONVERSION = "bad conversion";
  78. const char* const BAD_DEREFERENCE = "bad dereference";
  79. const char* const BAD_SUBSCRIPT = "operator[] call on a scalar";
  80. const char* const BAD_PUSHBACK = "appending to a non-sequence";
  81. const char* const BAD_INSERT = "inserting in a non-convertible-to-map";
  82. const char* const UNMATCHED_GROUP_TAG = "unmatched group tag";
  83. const char* const UNEXPECTED_END_SEQ = "unexpected end sequence token";
  84. const char* const UNEXPECTED_END_MAP = "unexpected end map token";
  85. const char* const SINGLE_QUOTED_CHAR =
  86. "invalid character in single-quoted string";
  87. const char* const INVALID_ANCHOR = "invalid anchor";
  88. const char* const INVALID_ALIAS = "invalid alias";
  89. const char* const INVALID_TAG = "invalid tag";
  90. const char* const BAD_FILE = "bad file";
  91. template <typename T>
  92. inline const std::string KEY_NOT_FOUND_WITH_KEY(
  93. const T&, typename disable_if<is_numeric<T>>::type* = 0) {
  94. return KEY_NOT_FOUND;
  95. }
  96. inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
  97. std::stringstream stream;
  98. stream << KEY_NOT_FOUND << ": " << key;
  99. return stream.str();
  100. }
  101. template <typename T>
  102. inline const std::string KEY_NOT_FOUND_WITH_KEY(
  103. const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
  104. std::stringstream stream;
  105. stream << KEY_NOT_FOUND << ": " << key;
  106. return stream.str();
  107. }
  108. }
  109. class YAML_CPP_API Exception : public std::runtime_error {
  110. public:
  111. Exception(const Mark& mark_, const std::string& msg_)
  112. : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {}
  113. virtual ~Exception() YAML_CPP_NOEXCEPT;
  114. Exception(const Exception&) = default;
  115. Mark mark;
  116. std::string msg;
  117. private:
  118. static const std::string build_what(const Mark& mark,
  119. const std::string& msg) {
  120. if (mark.is_null()) {
  121. return msg.c_str();
  122. }
  123. std::stringstream output;
  124. output << "yaml-cpp: error at line " << mark.line + 1 << ", column "
  125. << mark.column + 1 << ": " << msg;
  126. return output.str();
  127. }
  128. };
  129. class YAML_CPP_API ParserException : public Exception {
  130. public:
  131. ParserException(const Mark& mark_, const std::string& msg_)
  132. : Exception(mark_, msg_) {}
  133. ParserException(const ParserException&) = default;
  134. virtual ~ParserException() YAML_CPP_NOEXCEPT;
  135. };
  136. class YAML_CPP_API RepresentationException : public Exception {
  137. public:
  138. RepresentationException(const Mark& mark_, const std::string& msg_)
  139. : Exception(mark_, msg_) {}
  140. RepresentationException(const RepresentationException&) = default;
  141. virtual ~RepresentationException() YAML_CPP_NOEXCEPT;
  142. };
  143. // representation exceptions
  144. class YAML_CPP_API InvalidScalar : public RepresentationException {
  145. public:
  146. InvalidScalar(const Mark& mark_)
  147. : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {}
  148. InvalidScalar(const InvalidScalar&) = default;
  149. virtual ~InvalidScalar() YAML_CPP_NOEXCEPT;
  150. };
  151. class YAML_CPP_API KeyNotFound : public RepresentationException {
  152. public:
  153. template <typename T>
  154. KeyNotFound(const Mark& mark_, const T& key_)
  155. : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {
  156. }
  157. KeyNotFound(const KeyNotFound&) = default;
  158. virtual ~KeyNotFound() YAML_CPP_NOEXCEPT;
  159. };
  160. template <typename T>
  161. class YAML_CPP_API TypedKeyNotFound : public KeyNotFound {
  162. public:
  163. TypedKeyNotFound(const Mark& mark_, const T& key_)
  164. : KeyNotFound(mark_, key_), key(key_) {}
  165. virtual ~TypedKeyNotFound() YAML_CPP_NOEXCEPT {}
  166. T key;
  167. };
  168. template <typename T>
  169. inline TypedKeyNotFound<T> MakeTypedKeyNotFound(const Mark& mark,
  170. const T& key) {
  171. return TypedKeyNotFound<T>(mark, key);
  172. }
  173. class YAML_CPP_API InvalidNode : public RepresentationException {
  174. public:
  175. InvalidNode()
  176. : RepresentationException(Mark::null_mark(), ErrorMsg::INVALID_NODE) {}
  177. InvalidNode(const InvalidNode&) = default;
  178. virtual ~InvalidNode() YAML_CPP_NOEXCEPT;
  179. };
  180. class YAML_CPP_API BadConversion : public RepresentationException {
  181. public:
  182. explicit BadConversion(const Mark& mark_)
  183. : RepresentationException(mark_, ErrorMsg::BAD_CONVERSION) {}
  184. BadConversion(const BadConversion&) = default;
  185. virtual ~BadConversion() YAML_CPP_NOEXCEPT;
  186. };
  187. template <typename T>
  188. class TypedBadConversion : public BadConversion {
  189. public:
  190. explicit TypedBadConversion(const Mark& mark_) : BadConversion(mark_) {}
  191. };
  192. class YAML_CPP_API BadDereference : public RepresentationException {
  193. public:
  194. BadDereference()
  195. : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_DEREFERENCE) {}
  196. BadDereference(const BadDereference&) = default;
  197. virtual ~BadDereference() YAML_CPP_NOEXCEPT;
  198. };
  199. class YAML_CPP_API BadSubscript : public RepresentationException {
  200. public:
  201. BadSubscript()
  202. : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_SUBSCRIPT) {}
  203. BadSubscript(const BadSubscript&) = default;
  204. virtual ~BadSubscript() YAML_CPP_NOEXCEPT;
  205. };
  206. class YAML_CPP_API BadPushback : public RepresentationException {
  207. public:
  208. BadPushback()
  209. : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_PUSHBACK) {}
  210. BadPushback(const BadPushback&) = default;
  211. virtual ~BadPushback() YAML_CPP_NOEXCEPT;
  212. };
  213. class YAML_CPP_API BadInsert : public RepresentationException {
  214. public:
  215. BadInsert()
  216. : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_INSERT) {}
  217. BadInsert(const BadInsert&) = default;
  218. virtual ~BadInsert() YAML_CPP_NOEXCEPT;
  219. };
  220. class YAML_CPP_API EmitterException : public Exception {
  221. public:
  222. EmitterException(const std::string& msg_)
  223. : Exception(Mark::null_mark(), msg_) {}
  224. EmitterException(const EmitterException&) = default;
  225. virtual ~EmitterException() YAML_CPP_NOEXCEPT;
  226. };
  227. class YAML_CPP_API BadFile : public Exception {
  228. public:
  229. BadFile() : Exception(Mark::null_mark(), ErrorMsg::BAD_FILE) {}
  230. BadFile(const BadFile&) = default;
  231. virtual ~BadFile() YAML_CPP_NOEXCEPT;
  232. };
  233. }
  234. #undef YAML_CPP_NOEXCEPT
  235. #endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66