MySQL 8.4.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#ifdef MYSQL_SERVER
76
78 public:
79 JsonParseDefaultErrorHandler(const char *func_name, int arg_idx)
80 : m_func_name(func_name), m_arg_idx(arg_idx) {}
81
82 void operator()(const char *parse_err, size_t err_offset) const;
83
84 private:
85 const char *m_func_name;
86 const int m_arg_idx;
87};
88
90
91/**
92 Error handler to be used when serializing JSON binary values in server code.
93 Uses my_error(), so it cannot be used in code outside of the server.
94*/
97 public:
98 explicit JsonSerializationDefaultErrorHandler(const THD *thd) : m_thd(thd) {}
99 void KeyTooBig() const override;
100 void ValueTooBig() const override;
101 void TooDeep() const override;
102 void InvalidJson() const override;
103 void InternalError(const char *message) const override;
104 bool CheckStack() const override;
105
106 private:
107 const THD *m_thd;
108};
109
110/// Callback function called when a coercion error occurs. It reports the
111/// error as ERROR
113 public:
114 explicit JsonCoercionErrorHandler(const char *msgnam) : m_msgnam(msgnam) {}
115 void operator()(const char *target_type, int error_code) const;
116
117 private:
118 /// The name of the field/expression being coerced to be used
119 /// in error message if conversion failed.
120 const char *m_msgnam;
121};
122
123/// Callback function called when a coercion error occurs. It reports the
124/// error as WARNING
126 public:
127 explicit JsonCoercionWarnHandler(const char *msgnam) : m_msgnam(msgnam) {}
128 void operator()(const char *target_type, int error_code) const;
129
130 private:
131 /// The name of the field/expression being coerced to be used
132 /// in error message if conversion failed.
133 const char *m_msgnam;
134};
135
136/// Callback function that checks if MYSQL_TIME_STATUS contains a deprecation
137/// warning. If it does, it issues the warning and resets the status indication.
139 public:
141};
142
143#endif // MYSQL_SERVER
144#endif // JSON_ERROR_HANDLER_INCLUDED
Callback function that checks if MYSQL_TIME_STATUS contains a deprecation warning.
Definition: json_error_handler.h:138
void operator()(MYSQL_TIME_STATUS &status) const
Definition: json_error_handler.cc:86
Callback function called when a coercion error occurs.
Definition: json_error_handler.h:112
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:120
void operator()(const char *target_type, int error_code) const
Definition: json_error_handler.cc:68
JsonCoercionErrorHandler(const char *msgnam)
Definition: json_error_handler.h:114
Callback function called when a coercion error occurs.
Definition: json_error_handler.h:125
void operator()(const char *target_type, int error_code) const
Definition: json_error_handler.cc:74
JsonCoercionWarnHandler(const char *msgnam)
Definition: json_error_handler.h:127
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:133
Definition: json_error_handler.h:77
const int m_arg_idx
Definition: json_error_handler.h:86
JsonParseDefaultErrorHandler(const char *func_name, int arg_idx)
Definition: json_error_handler.h:79
void operator()(const char *parse_err, size_t err_offset) const
Definition: json_error_handler.cc:35
const char * m_func_name
Definition: json_error_handler.h:85
Error handler to be used when serializing JSON binary values in server code.
Definition: json_error_handler.h:96
void TooDeep() const override
Called when a JSON document has more nesting levels than supported.
Definition: json_error_handler.cc:51
const THD * m_thd
Definition: json_error_handler.h:107
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:43
void InternalError(const char *message) const override
Called when an internal error occurs.
Definition: json_error_handler.cc:59
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:47
bool CheckStack() const override
Check if the stack is about to be exhausted, and report the error.
Definition: json_error_handler.cc:64
void InvalidJson() const override
Called when an invalid JSON value is encountered.
Definition: json_error_handler.cc:55
JsonSerializationDefaultErrorHandler(const THD *thd)
Definition: json_error_handler.h:98
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:41
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:423
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(),...
Definition: my_time.h:170