error.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef RAPIDJSON_ERROR_ERROR_H__
  15. #define RAPIDJSON_ERROR_ERROR_H__
  16. #include "../rapidjson.h"
  17. /*! \file error.h */
  18. /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // RAPIDJSON_ERROR_CHARTYPE
  21. //! Character type of error messages.
  22. /*! \ingroup RAPIDJSON_ERRORS
  23. The default character type is \c char.
  24. On Windows, user can define this macro as \c TCHAR for supporting both
  25. unicode/non-unicode settings.
  26. */
  27. #ifndef RAPIDJSON_ERROR_CHARTYPE
  28. #define RAPIDJSON_ERROR_CHARTYPE char
  29. #endif
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // RAPIDJSON_ERROR_STRING
  32. //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
  33. /*! \ingroup RAPIDJSON_ERRORS
  34. By default this conversion macro does nothing.
  35. On Windows, user can define this macro as \c _T(x) for supporting both
  36. unicode/non-unicode settings.
  37. */
  38. #ifndef RAPIDJSON_ERROR_STRING
  39. #define RAPIDJSON_ERROR_STRING(x) x
  40. #endif
  41. RAPIDJSON_NAMESPACE_BEGIN
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // ParseErrorCode
  44. //! Error code of parsing.
  45. /*! \ingroup RAPIDJSON_ERRORS
  46. \see GenericReader::Parse, GenericReader::GetParseErrorCode
  47. */
  48. enum ParseErrorCode {
  49. kParseErrorNone = 0, //!< No error.
  50. kParseErrorDocumentEmpty, //!< The document is empty.
  51. kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
  52. kParseErrorValueInvalid, //!< Invalid value.
  53. kParseErrorObjectMissName, //!< Missing a name for object member.
  54. kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
  55. kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
  56. kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
  57. kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
  58. kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
  59. kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
  60. kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
  61. kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
  62. kParseErrorNumberTooBig, //!< Number too big to be stored in double.
  63. kParseErrorNumberMissFraction, //!< Miss fraction part in number.
  64. kParseErrorNumberMissExponent, //!< Miss exponent in number.
  65. kParseErrorTermination, //!< Parsing was terminated.
  66. kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
  67. };
  68. //! Result of parsing (wraps ParseErrorCode)
  69. /*!
  70. \ingroup RAPIDJSON_ERRORS
  71. \code
  72. Document doc;
  73. ParseResult ok = doc.Parse("[42]");
  74. if (!ok) {
  75. fprintf(stderr, "JSON parse error: %s (%u)",
  76. GetParseError_En(ok.Code()), ok.Offset());
  77. exit(EXIT_FAILURE);
  78. }
  79. \endcode
  80. \see GenericReader::Parse, GenericDocument::Parse
  81. */
  82. struct ParseResult {
  83. //! Default constructor, no error.
  84. ParseResult() : code_(kParseErrorNone), offset_(0) {}
  85. //! Constructor to set an error.
  86. ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
  87. //! Get the error code.
  88. ParseErrorCode Code() const { return code_; }
  89. //! Get the error offset, if \ref IsError(), 0 otherwise.
  90. size_t Offset() const { return offset_; }
  91. //! Conversion to \c bool, returns \c true, iff !\ref IsError().
  92. operator bool() const { return !IsError(); }
  93. //! Whether the result is an error.
  94. bool IsError() const { return code_ != kParseErrorNone; }
  95. bool operator==(const ParseResult& that) const { return code_ == that.code_; }
  96. bool operator==(ParseErrorCode code) const { return code_ == code; }
  97. friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
  98. //! Reset error code.
  99. void Clear() { Set(kParseErrorNone); }
  100. //! Update error code and offset.
  101. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
  102. private:
  103. ParseErrorCode code_;
  104. size_t offset_;
  105. };
  106. //! Function pointer type of GetParseError().
  107. /*! \ingroup RAPIDJSON_ERRORS
  108. This is the prototype for \c GetParseError_X(), where \c X is a locale.
  109. User can dynamically change locale in runtime, e.g.:
  110. \code
  111. GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
  112. const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
  113. \endcode
  114. */
  115. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
  116. RAPIDJSON_NAMESPACE_END
  117. #endif // RAPIDJSON_ERROR_ERROR_H__