MySQL 9.0.0
Source Code Documentation
json_error_handler.h
Go to the documentation of this file.
1#ifndef JSON_ERROR_HANDLER_INCLUDED
2#define JSON_ERROR_HANDLER_INCLUDED
3
4/* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <cstdlib>
28#include <functional>
29
30class THD;
31struct CHARSET_INFO;
33
35 std::function<void(const char *parse_err, size_t err_offset)>;
36using JsonErrorHandler = std::function<void()>;
38 std::function<void(const char *target_type, int error_code)>;
40 std::function<void(MYSQL_TIME_STATUS &status)>;
41/**
42 Error handler for the functions that serialize a JSON value in the JSON binary
43 storage format. The member functions are called when an error occurs, and they
44 should report the error the way the caller has specified. When called from the
45 server, my_error() should be called to signal the error. The subclass
46 JsonSerializationDefaultErrorHandler, which calls my_error(), should be used
47 when called from server code.
48*/
50 public:
51 virtual ~JsonSerializationErrorHandler() = default;
52
53 /// Called when a JSON object contains a member with a name that is longer
54 /// than supported by the JSON binary format.
55 virtual void KeyTooBig() const = 0;
56
57 /// Called when a JSON document is too big to be stored in the JSON binary
58 /// format.
59 virtual void ValueTooBig() const = 0;
60
61 /// Called when a JSON document has more nesting levels than supported.
62 virtual void TooDeep() const = 0;
63
64 /// Called when an invalid JSON value is encountered.
65 virtual void InvalidJson() const = 0;
66
67 /// Called when an internal error occurs.
68 virtual void InternalError(const char *message) const = 0;
69
70 /// Check if the stack is about to be exhausted, and report the error.
71 /// @return true if the stack is about to be exhausted, false otherwise.
72 virtual bool CheckStack() const = 0;
73};
74
75/**
76 Error handler to be used when parsing JSON schemas and validating JSON
77 objects using a JSON schema.
78*/
80 public:
81 virtual ~JsonSchemaErrorHandler() = default;
82 /// Called when an invalid JSON value is encountered.
83 virtual void InvalidJsonText(size_t arg_no, const char *wrong_string,
84 size_t offset) const = 0;
85 /// Called if the provided JSON is not a JSON object.
86 virtual void InvalidJsonType() const = 0;
87 /// Called if a std exception is thrown
88 virtual void HandleStdExceptions() const = 0;
89 /// Called if a schema reference is encountered in the JSON document as
90 /// MySQL does not support such constructs.
91 virtual void NotSupported() const = 0;
92};
93
94#ifdef MYSQL_SERVER
95
97 public:
98 JsonParseDefaultErrorHandler(const char *func_name, int arg_idx)
99 : m_func_name(func_name), m_arg_idx(arg_idx) {}
100
101 void operator()(const char *parse_err, size_t err_offset) const;
102
103 private:
104 const char *m_func_name;
105 const int m_arg_idx;
106};
107
109
110/**
111 Error handler to be used when serializing JSON binary values in server code.
112 Uses my_error(), so it cannot be used in code outside of the server.
113*/
116 public:
117 explicit JsonSerializationDefaultErrorHandler(const THD *thd) : m_thd(thd) {}
118 void KeyTooBig() const override;
119 void ValueTooBig() const override;
120 void TooDeep() const override;
121 void InvalidJson() const override;
122 void InternalError(const char *message) const override;
123 bool CheckStack() const override;
124
125 private:
126 const THD *m_thd;
127};
128
129/**
130 The default error handler to be used when parsing JSON schemas and
131 validating JSON objects using a JSON schema inside the MySQL server.
132*/
134 public:
135 explicit JsonSchemaDefaultErrorHandler(const char *function_name)
136 : m_calling_function_name(function_name) {}
137 void InvalidJsonText(size_t arg_no, const char *wrong_string,
138 size_t offset) const override;
139 void InvalidJsonType() const override;
140 void HandleStdExceptions() const override;
141 void NotSupported() const override;
142
143 private:
144 /// Used for error reporting and holds the name of the function.
146};
147
148/// Callback function called when a coercion error occurs. It reports the
149/// error as ERROR
151 public:
152 explicit JsonCoercionErrorHandler(const char *msgnam) : m_msgnam(msgnam) {}
153 void operator()(const char *target_type, int error_code) const;
154
155 private:
156 /// The name of the field/expression being coerced to be used
157 /// in error message if conversion failed.
158 const char *m_msgnam;
159};
160
161/// Callback function called when a coercion error occurs. It reports the
162/// error as WARNING
164 public:
165 explicit JsonCoercionWarnHandler(const char *msgnam) : m_msgnam(msgnam) {}
166 void operator()(const char *target_type, int error_code) const;
167
168 private:
169 /// The name of the field/expression being coerced to be used
170 /// in error message if conversion failed.
171 const char *m_msgnam;
172};
173
174/// Callback function that checks if MYSQL_TIME_STATUS contains a deprecation
175/// warning. If it does, it issues the warning and resets the status indication.
177 public:
179};
180
181#endif // MYSQL_SERVER
182#endif // JSON_ERROR_HANDLER_INCLUDED
Callback function that checks if MYSQL_TIME_STATUS contains a deprecation warning.
Definition: json_error_handler.h:176
void operator()(MYSQL_TIME_STATUS &status) const
Definition: json_error_handler.cc:87
Callback function called when a coercion error occurs.
Definition: json_error_handler.h:150
const char * m_msgnam
The name of the field/expression being coerced to be used in error message if conversion failed.
Definition: json_error_handler.h:158
void operator()(const char *target_type, int error_code) const
Definition: json_error_handler.cc:69
JsonCoercionErrorHandler(const char *msgnam)
Definition: json_error_handler.h:152
Callback function called when a coercion error occurs.
Definition: json_error_handler.h:163
void operator()(const char *target_type, int error_code) const
Definition: json_error_handler.cc:75
JsonCoercionWarnHandler(const char *msgnam)
Definition: json_error_handler.h:165
const char * m_msgnam
The name of the field/expression being coerced to be used in error message if conversion failed.
Definition: json_error_handler.h:171
Definition: json_error_handler.h:96
const int m_arg_idx
Definition: json_error_handler.h:105
JsonParseDefaultErrorHandler(const char *func_name, int arg_idx)
Definition: json_error_handler.h:98
void operator()(const char *parse_err, size_t err_offset) const
Definition: json_error_handler.cc:36
const char * m_func_name
Definition: json_error_handler.h:104
The default error handler to be used when parsing JSON schemas and validating JSON objects using a JS...
Definition: json_error_handler.h:133
void InvalidJsonText(size_t arg_no, const char *wrong_string, size_t offset) const override
Called when an invalid JSON value is encountered.
Definition: json_error_handler.cc:92
void HandleStdExceptions() const override
Called if a std exception is thrown.
Definition: json_error_handler.cc:103
void InvalidJsonType() const override
Called if the provided JSON is not a JSON object.
Definition: json_error_handler.cc:107
JsonSchemaDefaultErrorHandler(const char *function_name)
Definition: json_error_handler.h:135
void NotSupported() const override
Called if a schema reference is encountered in the JSON document as MySQL does not support such const...
Definition: json_error_handler.cc:99
const char * m_calling_function_name
Used for error reporting and holds the name of the function.
Definition: json_error_handler.h:145
Error handler to be used when parsing JSON schemas and validating JSON objects using a JSON schema.
Definition: json_error_handler.h:79
virtual void InvalidJsonText(size_t arg_no, const char *wrong_string, size_t offset) const =0
Called when an invalid JSON value is encountered.
virtual void HandleStdExceptions() const =0
Called if a std exception is thrown.
virtual void NotSupported() const =0
Called if a schema reference is encountered in the JSON document as MySQL does not support such const...
virtual ~JsonSchemaErrorHandler()=default
virtual void InvalidJsonType() const =0
Called if the provided JSON is not a JSON object.
Error handler to be used when serializing JSON binary values in server code.
Definition: json_error_handler.h:115
void TooDeep() const override
Called when a JSON document has more nesting levels than supported.
Definition: json_error_handler.cc:52
const THD * m_thd
Definition: json_error_handler.h:126
void KeyTooBig() const override
Called when a JSON object contains a member with a name that is longer than supported by the JSON bin...
Definition: json_error_handler.cc:44
void InternalError(const char *message) const override
Called when an internal error occurs.
Definition: json_error_handler.cc:60
void ValueTooBig() const override
Called when a JSON document is too big to be stored in the JSON binary format.
Definition: json_error_handler.cc:48
bool CheckStack() const override
Check if the stack is about to be exhausted, and report the error.
Definition: json_error_handler.cc:65
void InvalidJson() const override
Called when an invalid JSON value is encountered.
Definition: json_error_handler.cc:56
JsonSerializationDefaultErrorHandler(const THD *thd)
Definition: json_error_handler.h:117
Error handler for the functions that serialize a JSON value in the JSON binary storage format.
Definition: json_error_handler.h:49
virtual void InvalidJson() const =0
Called when an invalid JSON value is encountered.
virtual void ValueTooBig() const =0
Called when a JSON document is too big to be stored in the JSON binary format.
virtual bool CheckStack() const =0
Check if the stack is about to be exhausted, and report the error.
virtual void KeyTooBig() const =0
Called when a JSON object contains a member with a name that is longer than supported by the JSON bin...
virtual ~JsonSerializationErrorHandler()=default
virtual void InternalError(const char *message) const =0
Called when an internal error occurs.
virtual void TooDeep() const =0
Called when a JSON document has more nesting levels than supported.
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
std::function< void(MYSQL_TIME_STATUS &status)> JsonCoercionDeprecatedHandler
Definition: json_error_handler.h:40
std::function< void(const char *target_type, int error_code)> JsonCoercionHandler
Definition: json_error_handler.h:38
void JsonDepthErrorHandler()
Definition: json_error_handler.cc:42
std::function< void()> JsonErrorHandler
Definition: json_error_handler.h:36
std::function< void(const char *parse_err, size_t err_offset)> JsonParseErrorHandler
Definition: json_error_handler.h:35
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
Definition: m_ctype.h:421
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(),...
Definition: my_time.h:170