error.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.
  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. #ifdef __clang__
  18. RAPIDJSON_DIAG_PUSH
  19. RAPIDJSON_DIAG_OFF(padded)
  20. #endif
  21. /*! \file error.h */
  22. /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // RAPIDJSON_ERROR_CHARTYPE
  25. //! Character type of error messages.
  26. /*! \ingroup RAPIDJSON_ERRORS
  27. The default character type is \c char.
  28. On Windows, user can define this macro as \c TCHAR for supporting both
  29. unicode/non-unicode settings.
  30. */
  31. #ifndef RAPIDJSON_ERROR_CHARTYPE
  32. #define RAPIDJSON_ERROR_CHARTYPE char
  33. #endif
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // RAPIDJSON_ERROR_STRING
  36. //! Macro for converting string literal to \ref RAPIDJSON_ERROR_CHARTYPE[].
  37. /*! \ingroup RAPIDJSON_ERRORS
  38. By default this conversion macro does nothing.
  39. On Windows, user can define this macro as \c _T(x) for supporting both
  40. unicode/non-unicode settings.
  41. */
  42. #ifndef RAPIDJSON_ERROR_STRING
  43. #define RAPIDJSON_ERROR_STRING(x) x
  44. #endif
  45. RAPIDJSON_NAMESPACE_BEGIN
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // ParseErrorCode
  48. //! Error code of parsing.
  49. /*! \ingroup RAPIDJSON_ERRORS
  50. \see GenericReader::Parse, GenericReader::GetParseErrorCode
  51. */
  52. enum ParseErrorCode {
  53. kParseErrorNone = 0, //!< No error.
  54. kParseErrorDocumentEmpty, //!< The document is empty.
  55. kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
  56. kParseErrorValueInvalid, //!< Invalid value.
  57. kParseErrorObjectMissName, //!< Missing a name for object member.
  58. kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
  59. kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
  60. kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
  61. kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
  62. kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
  63. kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
  64. kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
  65. kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
  66. kParseErrorNumberTooBig, //!< Number too big to be stored in double.
  67. kParseErrorNumberMissFraction, //!< Miss fraction part in number.
  68. kParseErrorNumberMissExponent, //!< Miss exponent in number.
  69. kParseErrorTermination, //!< Parsing was terminated.
  70. kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
  71. };
  72. //! Result of parsing (wraps ParseErrorCode)
  73. /*!
  74. \ingroup RAPIDJSON_ERRORS
  75. \code
  76. Document doc;
  77. ParseResult ok = doc.Parse("[42]");
  78. if (!ok) {
  79. fprintf(stderr, "JSON parse error: %s (%u)",
  80. GetParseError_En(ok.Code()), ok.Offset());
  81. exit(EXIT_FAILURE);
  82. }
  83. \endcode
  84. \see GenericReader::Parse, GenericDocument::Parse
  85. */
  86. struct ParseResult {
  87. //!! Unspecified boolean type
  88. typedef bool (ParseResult::*BooleanType)() const;
  89. public:
  90. //! Default constructor, no error.
  91. ParseResult() : code_(kParseErrorNone), offset_(0) {}
  92. //! Constructor to set an error.
  93. ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
  94. //! Get the error code.
  95. ParseErrorCode Code() const { return code_; }
  96. //! Get the error offset, if \ref IsError(), 0 otherwise.
  97. size_t Offset() const { return offset_; }
  98. //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError().
  99. operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; }
  100. //! Whether the result is an error.
  101. bool IsError() const { return code_ != kParseErrorNone; }
  102. bool operator==(const ParseResult& that) const { return code_ == that.code_; }
  103. bool operator==(ParseErrorCode code) const { return code_ == code; }
  104. friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
  105. bool operator!=(const ParseResult& that) const { return !(*this == that); }
  106. bool operator!=(ParseErrorCode code) const { return !(*this == code); }
  107. friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; }
  108. //! Reset error code.
  109. void Clear() { Set(kParseErrorNone); }
  110. //! Update error code and offset.
  111. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
  112. private:
  113. ParseErrorCode code_;
  114. size_t offset_;
  115. };
  116. //! Function pointer type of GetParseError().
  117. /*! \ingroup RAPIDJSON_ERRORS
  118. This is the prototype for \c GetParseError_X(), where \c X is a locale.
  119. User can dynamically change locale in runtime, e.g.:
  120. \code
  121. GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
  122. const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
  123. \endcode
  124. */
  125. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // ValidateErrorCode
  128. //! Error codes when validating.
  129. /*! \ingroup RAPIDJSON_ERRORS
  130. \see GenericSchemaValidator
  131. */
  132. enum ValidateErrorCode {
  133. kValidateErrors = -1, //!< Top level error code when kValidateContinueOnErrorsFlag set.
  134. kValidateErrorNone = 0, //!< No error.
  135. kValidateErrorMultipleOf, //!< Number is not a multiple of the 'multipleOf' value.
  136. kValidateErrorMaximum, //!< Number is greater than the 'maximum' value.
  137. kValidateErrorExclusiveMaximum, //!< Number is greater than or equal to the 'maximum' value.
  138. kValidateErrorMinimum, //!< Number is less than the 'minimum' value.
  139. kValidateErrorExclusiveMinimum, //!< Number is less than or equal to the 'minimum' value.
  140. kValidateErrorMaxLength, //!< String is longer than the 'maxLength' value.
  141. kValidateErrorMinLength, //!< String is longer than the 'maxLength' value.
  142. kValidateErrorPattern, //!< String does not match the 'pattern' regular expression.
  143. kValidateErrorMaxItems, //!< Array is longer than the 'maxItems' value.
  144. kValidateErrorMinItems, //!< Array is shorter than the 'minItems' value.
  145. kValidateErrorUniqueItems, //!< Array has duplicate items but 'uniqueItems' is true.
  146. kValidateErrorAdditionalItems, //!< Array has additional items that are not allowed by the schema.
  147. kValidateErrorMaxProperties, //!< Object has more members than 'maxProperties' value.
  148. kValidateErrorMinProperties, //!< Object has less members than 'minProperties' value.
  149. kValidateErrorRequired, //!< Object is missing one or more members required by the schema.
  150. kValidateErrorAdditionalProperties, //!< Object has additional members that are not allowed by the schema.
  151. kValidateErrorPatternProperties, //!< See other errors.
  152. kValidateErrorDependencies, //!< Object has missing property or schema dependencies.
  153. kValidateErrorEnum, //!< Property has a value that is not one of its allowed enumerated values.
  154. kValidateErrorType, //!< Property has a type that is not allowed by the schema.
  155. kValidateErrorOneOf, //!< Property did not match any of the sub-schemas specified by 'oneOf'.
  156. kValidateErrorOneOfMatch, //!< Property matched more than one of the sub-schemas specified by 'oneOf'.
  157. kValidateErrorAllOf, //!< Property did not match all of the sub-schemas specified by 'allOf'.
  158. kValidateErrorAnyOf, //!< Property did not match any of the sub-schemas specified by 'anyOf'.
  159. kValidateErrorNot, //!< Property matched the sub-schema specified by 'not'.
  160. kValidateErrorReadOnly, //!< Property is read-only but has been provided when validation is for writing
  161. kValidateErrorWriteOnly //!< Property is write-only but has been provided when validation is for reading
  162. };
  163. //! Function pointer type of GetValidateError().
  164. /*! \ingroup RAPIDJSON_ERRORS
  165. This is the prototype for \c GetValidateError_X(), where \c X is a locale.
  166. User can dynamically change locale in runtime, e.g.:
  167. \code
  168. GetValidateErrorFunc GetValidateError = GetValidateError_En; // or whatever
  169. const RAPIDJSON_ERROR_CHARTYPE* s = GetValidateError(validator.GetInvalidSchemaCode());
  170. \endcode
  171. */
  172. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetValidateErrorFunc)(ValidateErrorCode);
  173. ///////////////////////////////////////////////////////////////////////////////
  174. // SchemaErrorCode
  175. //! Error codes when validating.
  176. /*! \ingroup RAPIDJSON_ERRORS
  177. \see GenericSchemaValidator
  178. */
  179. enum SchemaErrorCode {
  180. kSchemaErrorNone = 0, //!< No error.
  181. kSchemaErrorStartUnknown, //!< Pointer to start of schema does not resolve to a location in the document
  182. kSchemaErrorRefPlainName, //!< $ref fragment must be a JSON pointer
  183. kSchemaErrorRefInvalid, //!< $ref must not be an empty string
  184. kSchemaErrorRefPointerInvalid, //!< $ref fragment is not a valid JSON pointer at offset
  185. kSchemaErrorRefUnknown, //!< $ref does not resolve to a location in the target document
  186. kSchemaErrorRefCyclical, //!< $ref is cyclical
  187. kSchemaErrorRefNoRemoteProvider, //!< $ref is remote but there is no remote provider
  188. kSchemaErrorRefNoRemoteSchema, //!< $ref is remote but the remote provider did not return a schema
  189. kSchemaErrorRegexInvalid, //!< Invalid regular expression in 'pattern' or 'patternProperties'
  190. kSchemaErrorSpecUnknown, //!< JSON schema draft or OpenAPI version is not recognized
  191. kSchemaErrorSpecUnsupported, //!< JSON schema draft or OpenAPI version is not supported
  192. kSchemaErrorSpecIllegal, //!< Both JSON schema draft and OpenAPI version found in document
  193. kSchemaErrorReadOnlyAndWriteOnly //!< Property must not be both 'readOnly' and 'writeOnly'
  194. };
  195. //! Function pointer type of GetSchemaError().
  196. /*! \ingroup RAPIDJSON_ERRORS
  197. This is the prototype for \c GetSchemaError_X(), where \c X is a locale.
  198. User can dynamically change locale in runtime, e.g.:
  199. \code
  200. GetSchemaErrorFunc GetSchemaError = GetSchemaError_En; // or whatever
  201. const RAPIDJSON_ERROR_CHARTYPE* s = GetSchemaError(validator.GetInvalidSchemaCode());
  202. \endcode
  203. */
  204. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetSchemaErrorFunc)(SchemaErrorCode);
  205. ///////////////////////////////////////////////////////////////////////////////
  206. // PointerParseErrorCode
  207. //! Error code of JSON pointer parsing.
  208. /*! \ingroup RAPIDJSON_ERRORS
  209. \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode
  210. */
  211. enum PointerParseErrorCode {
  212. kPointerParseErrorNone = 0, //!< The parse is successful
  213. kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/'
  214. kPointerParseErrorInvalidEscape, //!< Invalid escape
  215. kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment
  216. kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment
  217. };
  218. //! Function pointer type of GetPointerParseError().
  219. /*! \ingroup RAPIDJSON_ERRORS
  220. This is the prototype for \c GetPointerParseError_X(), where \c X is a locale.
  221. User can dynamically change locale in runtime, e.g.:
  222. \code
  223. GetPointerParseErrorFunc GetPointerParseError = GetPointerParseError_En; // or whatever
  224. const RAPIDJSON_ERROR_CHARTYPE* s = GetPointerParseError(pointer.GetParseErrorCode());
  225. \endcode
  226. */
  227. typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetPointerParseErrorFunc)(PointerParseErrorCode);
  228. RAPIDJSON_NAMESPACE_END
  229. #ifdef __clang__
  230. RAPIDJSON_DIAG_POP
  231. #endif
  232. #endif // RAPIDJSON_ERROR_ERROR_H_