MySQL 9.1.0
Source Code Documentation
error_handler.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef ERROR_HANDLER_INCLUDED
25#define ERROR_HANDLER_INCLUDED
26
27#include <string>
28
29#include <stddef.h>
30#include <sys/types.h>
31
32#include "mysqld_error.h" // ER_*
33#include "sql/sql_error.h" // Sql_condition
34
35class Create_field;
36class Field;
37class String;
38class THD;
39class Table_ref;
40class handler;
41
42/**
43 This class represents the interface for internal error handlers.
44 Internal error handlers are exception handlers used by the server
45 implementation.
46*/
48 protected:
50
53 }
54
55 virtual ~Internal_error_handler() = default;
56
57 public:
58 /**
59 Handle a sql condition.
60 This method can be implemented by a subclass to achieve any of the
61 following:
62 - mask a warning/error internally, prevent exposing it to the user,
63 - mask a warning/error and throw another one instead.
64 When this method returns true, the sql condition is considered
65 'handled', and will not be propagated to upper layers.
66 It is the responsibility of the code installing an internal handler
67 to then check for trapped conditions, and implement logic to recover
68 from the anticipated conditions trapped during runtime.
69
70 This mechanism is similar to C++ try/throw/catch:
71 - 'try' correspond to <code>THD::push_internal_handler()</code>,
72 - 'throw' correspond to <code>my_error()</code>,
73 which invokes <code>my_message_sql()</code>,
74 - 'catch' correspond to checking how/if an internal handler was invoked,
75 before removing it from the exception stack with
76 <code>THD::pop_internal_handler()</code>.
77
78 @param thd the calling thread
79 @param sql_errno the error number for the condition raised.
80 @param sqlstate the SQLSTATE for the condition raised.
81 @param level the severity level for the condition raised.
82 @param msg the error message for the condition raised.
83 @return true if the condition is handled
84 */
85 virtual bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate,
87 const char *msg) = 0;
88
89 private:
91 friend class THD;
92};
93
94/**
95 Implements the trivial error handler which cancels all error states
96 and prevents an SQLSTATE to be set.
97*/
98
100 public:
101 bool handle_condition(THD *, uint, const char *,
103 const char *) override {
104 /* Ignore error */
105 return true;
106 }
107};
108
109/**
110 Implements the error handler for SET_VAR hint.
111 For Sys_var_hint::update_vars handler accepts first warning or error.
112 Subsequent error are ignored to avoid message duplication.
113 For Sys_var_hint::restore_vars all warnings and errors are ignored
114 since valid value is restored.
115*/
116
118 public:
119 Set_var_error_handler(bool ignore_warn_arg)
121 ignore_warn(ignore_warn_arg),
123
124 bool handle_condition(THD *, uint, const char *,
126 const char *) override {
127 if (*level == Sql_condition::SL_ERROR) (*level) = Sql_condition::SL_WARNING;
128
129 if (ignore_subsequent_messages) return true;
131
132 return ignore_warn;
133 }
134
136
137 private:
140};
141
142/**
143 This class is an internal error handler implementation for
144 DROP TABLE statements. The thing is that there may be warnings during
145 execution of these statements, which should not be exposed to the user.
146 This class is intended to silence such warnings.
147*/
148
150 public:
151 bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate,
153 const char *msg) override;
154};
155
156/**
157 Internal error handler to process an error from MDL_context::upgrade_lock()
158 and mysql_lock_tables(). Used by implementations of HANDLER READ and
159 LOCK TABLES LOCAL.
160*/
161
163 : public Internal_error_handler {
164 public:
165 bool handle_condition(THD *, uint sql_errno, const char *,
167 const char *) override {
168 if (sql_errno == ER_LOCK_ABORTED || sql_errno == ER_LOCK_DEADLOCK)
169 m_need_reopen = true;
170
171 return m_need_reopen;
172 }
173
174 bool need_reopen() const { return m_need_reopen; }
175 void init() { m_need_reopen = false; }
176
177 private:
179};
180
181/**
182 An Internal_error_handler that suppresses errors regarding views'
183 underlying tables that occur during privilege checking. It hides errors which
184 show view underlying table information.
185 This happens in the cases when
186
187 - A view's underlying table (e.g. referenced in its SELECT list) does not
188 exist or columns of underlying table are altered. There should not be an
189 error as no attempt was made to access it per se.
190
191 - Access is denied for some table, column, function or stored procedure
192 such as mentioned above. This error gets raised automatically, since we
193 can't untangle its access checking from that of the view itself.
194
195 There are currently two mechanisms at work that handle errors for views
196 based on an Internal_error_handler. This one and another one is
197 Show_create_error_handler. The latter handles errors encountered during
198 execution of SHOW CREATE VIEW, while this mechanism using this method is
199 handles SELECT from views. The two methods should not clash.
200
201*/
204
205 public:
206 View_error_handler(Table_ref *top_view) : m_top_view(top_view) {}
207 bool handle_condition(THD *thd, uint sql_errno, const char *,
209 const char *message) override;
210};
211
212/**
213 This internal handler is used to trap ER_NO_SUCH_TABLE and ER_BAD_DB_ERROR.
214*/
215
217 public:
219
220 bool handle_condition(THD *, uint sql_errno, const char *,
222 const char *) override {
223 if (sql_errno == ER_BAD_DB_ERROR || sql_errno == ER_NO_SUCH_TABLE) {
225 return true;
226 }
227
229 return false;
230 }
231
232 /**
233 Returns true if one or more ER_NO_SUCH_TABLE and ER_BAD_DB_ERROR errors have
234 been trapped and no other errors have been seen. false otherwise.
235 */
237 /*
238 If m_unhandled_errors != 0, something else, unanticipated, happened,
239 so the error is not trapped but returned to the caller.
240 Multiple ER_NO_SUCH_TABLE and ER_BAD_DB_ERROR can be raised in case of
241 views.
242 */
243 return ((m_handled_errors > 0) && (m_unhandled_errors == 0));
244 }
245
246 private:
249};
250
251/**
252 This internal handler implements downgrade from SL_ERROR to SL_WARNING
253 for statements which support IGNORE.
254*/
255
257 public:
258 bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate,
260 const char *msg) override;
261};
262
263/**
264 This internal handler implements upgrade from SL_WARNING to SL_ERROR
265 for the error codes affected by STRICT mode. Currently STRICT mode does
266 not affect SELECT statements.
267*/
268
270 public:
274 };
275
278
280 : m_set_select_behavior(param) {}
281
282 bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate,
284 const char *msg) override;
285
286 private:
287 /*
288 For SELECT and SET statement, we do not always give error in STRICT mode.
289 For triggers, Strict_error_handler is pushed in the beginning of statement.
290 If a SELECT or SET is executed from the Trigger, it should not always give
291 error. We use this flag to choose when to give error and when warning.
292 */
294};
295
296/**
297 The purpose of this error handler is to print out more user friendly error
298 messages when an error regarding a functional index happens. Since functional
299 indexes are implemented as hidden generated columns with an auto-generated
300 name, we would end up printing errors like "Out of range value for column
301 '912ec803b2ce49e4a541068d495ab570' at row 0". With this error handler, we
302 end up printing something like "Out of range value for functional index
303 'functional_index_2' at row 0" instead.
304
305 The handler keeps track of the previous error handler that was in use, and
306 calls that error handler to get the correct severity among other things.
307*/
309 public:
310 Functional_index_error_handler(const Field *field, THD *thd);
311
313 const std::string &functional_index_name,
314 THD *thd);
315
316 Functional_index_error_handler(const std::string &functional_index_name,
317 THD *thd);
318
319 bool handle_condition(THD *thd, uint sql_errno, const char *,
321 const char *message) override;
322
324
325 void force_error_code(int error_code) { m_force_error_code = error_code; }
326
327 private:
332};
333
334//////////////////////////////////////////////////////////////////////////
335
336/**
337 After retrieving the tablespace name, the tablespace name is validated.
338 If the name is invalid, it is ignored. The function used to validate
339 the name, 'validate_tablespace_name()', emits errors. In the context of
340 retrieving tablespace names, the errors must be ignored. This error handler
341 makes sure this is done.
342*/
343
345 public:
346 bool handle_condition(THD *, uint sql_errno, const char *,
348 const char *) override {
349 return (sql_errno == ER_WRONG_TABLESPACE_NAME ||
350 sql_errno == ER_TOO_LONG_IDENT);
351 }
352};
353
354/*
355 Disable ER_TOO_LONG_KEY for creation of system tables.
356 TODO: This is a Workaround due to bug#20629014.
357 Remove this internal error handler when the bug is fixed.
358*/
360 public:
361 bool handle_condition(THD *, uint sql_errno, const char *,
363 const char *) override {
364 return (sql_errno == ER_TOO_LONG_KEY);
365 }
366};
367
368/**
369 Error handler class to convert ER_LOCK_DEADLOCK error to
370 ER_WARN_I_S_SKIPPED_TABLE/TABLESPACE error.
371
372 Handler is pushed for opening a table or acquiring a MDL lock on
373 tables for INFORMATION_SCHEMA views (system views) operations.
374*/
376 public:
377 Info_schema_error_handler(THD *thd, const String *schema_name,
378 const String *table_name);
379
380 Info_schema_error_handler(THD *thd, const String *tablespace_name);
381
382 bool handle_condition(THD *, uint sql_errno, const char *,
384 const char *) override;
385
386 bool is_error_handled() const { return m_error_handled; }
387
388 private:
390
391 // Schema name
393
394 // Table name
396
397 // Tablespace name
399
402
403 // Flag to indicate whether deadlock error is handled by the handler or not.
404 bool m_error_handled = false;
405};
406
407/**
408 An Internal_error_handler that prevents revealing parent and child tables
409 information when the foreign key constraint check fails and user does not
410 have privileges to access those tables.
411*/
415
416 public:
418 : m_table_handler(table_handler), m_thd(thd) {}
419 bool handle_condition(THD *, uint sql_errno, const char *,
421 const char *message) override;
422};
423
424/// An error handler that silences all warnings.
426 public:
427 bool handle_condition(THD *, unsigned, const char *,
429 const char *) override {
430 return *level == Sql_condition::SL_WARNING;
431 }
432};
433
434/// An error handler which downgrades JSON syntax errors to warnings.
436 public:
439 bool handle_condition(THD *, uint, const char *,
441 const char *) override;
442
443 private:
446};
447
448#endif // ERROR_HANDLER_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
This class is an internal error handler implementation for DROP TABLE statements.
Definition: error_handler.h:149
bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg) override
Implementation of Drop_table_error_handler::handle_condition().
Definition: error_handler.cc:57
Implements the trivial error handler which cancels all error states and prevents an SQLSTATE to be se...
Definition: error_handler.h:99
bool handle_condition(THD *, uint, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.h:101
Definition: field.h:577
An Internal_error_handler that prevents revealing parent and child tables information when the foreig...
Definition: error_handler.h:412
THD * m_thd
Definition: error_handler.h:414
Foreign_key_error_handler(THD *thd, handler *table_handler)
Definition: error_handler.h:417
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *level, const char *message) override
Handle a sql condition.
Definition: error_handler.cc:497
handler * m_table_handler
Definition: error_handler.h:413
The purpose of this error handler is to print out more user friendly error messages when an error reg...
Definition: error_handler.h:308
Functional_index_error_handler(const Field *field, THD *thd)
Definition: error_handler.cc:229
~Functional_index_error_handler() override
Definition: error_handler.cc:283
bool m_pop_error_handler
Definition: error_handler.h:330
void force_error_code(int error_code)
Definition: error_handler.h:325
bool handle_condition(THD *thd, uint sql_errno, const char *, Sql_condition::enum_severity_level *level, const char *message) override
Handle a sql condition.
Definition: error_handler.cc:329
THD * m_thd
Definition: error_handler.h:329
int m_force_error_code
Definition: error_handler.h:331
std::string m_functional_index_name
Definition: error_handler.h:328
This internal handler implements downgrade from SL_ERROR to SL_WARNING for statements which support I...
Definition: error_handler.h:256
bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg) override
This handler is used for the statements which support IGNORE keyword.
Definition: error_handler.cc:73
An error handler which downgrades JSON syntax errors to warnings.
Definition: error_handler.h:435
bool handle_condition(THD *, uint, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.cc:544
Ignore_json_syntax_handler(THD *thd, bool enabled)
Definition: error_handler.cc:535
~Ignore_json_syntax_handler() override
Definition: error_handler.cc:540
bool m_enabled
Definition: error_handler.h:445
THD * m_thd
Definition: error_handler.h:444
An error handler that silences all warnings.
Definition: error_handler.h:425
bool handle_condition(THD *, unsigned, const char *, Sql_condition::enum_severity_level *level, const char *) override
Definition: error_handler.h:427
Error handler class to convert ER_LOCK_DEADLOCK error to ER_WARN_I_S_SKIPPED_TABLE/TABLESPACE error.
Definition: error_handler.h:375
bool m_error_handled
Definition: error_handler.h:404
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.cc:478
Mdl_object_type
Definition: error_handler.h:400
Mdl_object_type m_object_type
Definition: error_handler.h:401
const String * m_schema_name
Definition: error_handler.h:392
Info_schema_error_handler(THD *thd, const String *schema_name, const String *table_name)
Following are implementation of error handler to convert ER_LOCK_DEADLOCK error when executing I_S....
Definition: error_handler.cc:464
bool is_error_handled() const
Definition: error_handler.h:386
const String * m_tablespace_name
Definition: error_handler.h:398
const String * m_table_name
Definition: error_handler.h:395
bool m_can_deadlock
Definition: error_handler.h:389
This class represents the interface for internal error handlers.
Definition: error_handler.h:47
virtual ~Internal_error_handler()=default
Internal_error_handler()
Definition: error_handler.h:49
Internal_error_handler * prev_internal_handler() const
Definition: error_handler.h:51
Internal_error_handler * m_prev_internal_handler
Definition: error_handler.h:90
virtual bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg)=0
Handle a sql condition.
Definition: error_handler.h:359
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.h:361
Internal error handler to process an error from MDL_context::upgrade_lock() and mysql_lock_tables().
Definition: error_handler.h:163
void init()
Definition: error_handler.h:175
bool m_need_reopen
Definition: error_handler.h:178
bool need_reopen() const
Definition: error_handler.h:174
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.h:165
This internal handler is used to trap ER_NO_SUCH_TABLE and ER_BAD_DB_ERROR.
Definition: error_handler.h:216
bool safely_trapped_errors() const
Returns true if one or more ER_NO_SUCH_TABLE and ER_BAD_DB_ERROR errors have been trapped and no othe...
Definition: error_handler.h:236
int m_handled_errors
Definition: error_handler.h:247
int m_unhandled_errors
Definition: error_handler.h:248
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.h:220
No_such_table_error_handler()
Definition: error_handler.h:218
Implements the error handler for SET_VAR hint.
Definition: error_handler.h:117
Set_var_error_handler(bool ignore_warn_arg)
Definition: error_handler.h:119
bool handle_condition(THD *, uint, const char *, Sql_condition::enum_severity_level *level, const char *) override
Handle a sql condition.
Definition: error_handler.h:124
bool ignore_subsequent_messages
Definition: error_handler.h:139
bool ignore_warn
Definition: error_handler.h:138
void reset_state()
Definition: error_handler.h:135
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:63
@ SL_ERROR
Definition: sql_error.h:63
@ SL_WARNING
Definition: sql_error.h:63
This internal handler implements upgrade from SL_WARNING to SL_ERROR for the error codes affected by ...
Definition: error_handler.h:269
Strict_error_handler(enum_set_select_behavior param)
Definition: error_handler.h:279
enum_set_select_behavior
Definition: error_handler.h:271
@ DISABLE_SET_SELECT_STRICT_ERROR_HANDLER
Definition: error_handler.h:272
@ ENABLE_SET_SELECT_STRICT_ERROR_HANDLER
Definition: error_handler.h:273
Strict_error_handler()
Definition: error_handler.h:276
bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg) override
Implementation of STRICT mode.
Definition: error_handler.cc:154
enum_set_select_behavior m_set_select_behavior
Definition: error_handler.h:293
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2900
After retrieving the tablespace name, the tablespace name is validated.
Definition: error_handler.h:344
bool handle_condition(THD *, uint sql_errno, const char *, Sql_condition::enum_severity_level *, const char *) override
Handle a sql condition.
Definition: error_handler.h:346
An Internal_error_handler that suppresses errors regarding views' underlying tables that occur during...
Definition: error_handler.h:202
Table_ref * m_top_view
Definition: error_handler.h:203
View_error_handler(Table_ref *top_view)
Definition: error_handler.h:206
bool handle_condition(THD *thd, uint sql_errno, const char *, Sql_condition::enum_severity_level *level, const char *message) override
Handle a sql condition.
Definition: error_handler.cc:116
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4583
const char * table_name
Definition: rules_table_service.cc:56
required bool enabled
Definition: replication_group_member_actions.proto:33