MySQL 9.3.0
Source Code Documentation
polyglot_error.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_ERROR_H_
27#define MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_ERROR_H_
28
30
31#include <exception>
32#include <optional>
33#include <string>
34#include <utility>
35#include <vector>
36
39
40namespace shcore {
41namespace polyglot {
42
43inline const char *k_key_message{"message"};
44
45/**
46 * This class is used to represent errors occurred in the polyglot library while
47 * executing one of the functions in the API. These errors don't have more
48 * details, in general can be caused for 2 reasons:
49 *
50 * - The polyglot context got closed before executing the function.
51 * - En error occurred on the guest language while executing the operation.
52 */
53class Polyglot_generic_error : public std::exception {
54 public:
55 explicit Polyglot_generic_error(const std::string &msg) : m_message{msg} {}
56
57 const char *what() const noexcept override { return m_message.c_str(); }
58
59 const std::string &message() const { return m_message; }
60
61 protected:
63 virtual void set_message(const std::string &msg) { m_message = msg; }
64
65 private:
66 std::string m_message;
67};
68
69/**
70 * Represents polyglot errors that will be created from information available in
71 * the polyglot library state, it handles cases like:
72 *
73 * - poly_get_last_error_info
74 * - poly_exception_is_interrupted
75 * - poly_exception_has_object: like error objects from the shcore::Exception
76 */
78 public:
79 Polyglot_error(poly_thread thread, int64_t rc);
80 Polyglot_error(poly_thread thread, poly_exception exc);
81
82 std::string format(bool include_location = false) const;
83
84 bool is_interrupted() const { return m_interrupted; }
86 bool is_syntax_error() const;
87
89 std::optional<std::string> type() const { return m_type; }
90 std::optional<uint64_t> line() const { return m_line; }
91 std::optional<uint64_t> column() const { return m_column; }
92 std::optional<std::string> source_line() const { return m_source_line; }
93 std::optional<int64_t> code() const { return m_code; }
94 std::optional<std::string> source() const { return m_source; }
95 std::vector<std::string> backtrace() const { return m_backtrace; }
96
97 private:
98 void parse_and_translate(const std::string &source);
99
100 void initialize(poly_thread thread, poly_exception exc);
101 void initialize(poly_thread thread);
102 void set_message(const std::string &msg) override;
103
104 std::optional<std::string> m_type;
105 std::optional<size_t> m_line;
106 std::optional<size_t> m_column;
107 std::optional<std::string> m_source_line;
108 std::optional<int64_t> m_code;
109 std::optional<std::string> m_source;
110 std::vector<std::string> m_backtrace;
111 bool m_interrupted = false;
113};
114
115template <typename F, typename... Args>
116inline void throw_if_error(F f, poly_thread thread, Args &&...args) {
117 if (const auto rc = f(thread, std::forward<Args>(args)...); poly_ok != rc)
118 [[unlikely]] {
119 throw Polyglot_error(thread, rc);
120 }
121}
122
123} // namespace polyglot
124} // namespace shcore
125
126#endif // MYSQLSHDK_SCRIPTING_POLYGLOT_UTILS_POLYGLOT_ERROR_H_
Represents polyglot errors that will be created from information available in the polyglot library st...
Definition: polyglot_error.h:77
Polyglot_error(poly_thread thread, int64_t rc)
Definition: polyglot_error.cc:191
std::optional< int64_t > code() const
Definition: polyglot_error.h:93
bool is_syntax_error() const
Definition: polyglot_error.cc:393
std::optional< uint64_t > column() const
Definition: polyglot_error.h:91
std::string format(bool include_location=false) const
Definition: polyglot_error.cc:397
void parse_and_translate(const std::string &source)
This function is used to normalize as much as possible the exceptions, i.e.
Definition: polyglot_error.cc:338
std::optional< size_t > m_column
Definition: polyglot_error.h:106
std::optional< uint64_t > line() const
Definition: polyglot_error.h:90
std::optional< int64_t > m_code
Definition: polyglot_error.h:108
std::optional< std::string > source() const
Definition: polyglot_error.h:94
std::optional< std::string > m_source_line
Definition: polyglot_error.h:107
shcore::Dictionary_t data() const
Definition: polyglot_error.cc:456
std::optional< std::string > source_line() const
Definition: polyglot_error.h:92
std::optional< std::string > type() const
Definition: polyglot_error.h:89
bool is_resource_exhausted() const
Definition: polyglot_error.h:85
std::vector< std::string > backtrace() const
Definition: polyglot_error.h:95
bool is_interrupted() const
Definition: polyglot_error.h:84
std::optional< std::string > m_source
Definition: polyglot_error.h:109
std::vector< std::string > m_backtrace
Definition: polyglot_error.h:110
std::optional< size_t > m_line
Definition: polyglot_error.h:105
std::optional< std::string > m_type
Definition: polyglot_error.h:104
void initialize(poly_thread thread, poly_exception exc)
Definition: polyglot_error.cc:209
void set_message(const std::string &msg) override
Definition: polyglot_error.cc:321
bool m_resource_exhausted
Definition: polyglot_error.h:112
bool m_interrupted
Definition: polyglot_error.h:111
This class is used to represent errors occurred in the polyglot library while executing one of the fu...
Definition: polyglot_error.h:53
virtual void set_message(const std::string &msg)
Definition: polyglot_error.h:63
std::string m_message
Definition: polyglot_error.h:66
Polyglot_generic_error(const std::string &msg)
Definition: polyglot_error.h:55
const std::string & message() const
Definition: polyglot_error.h:59
const char * what() const noexcept override
Definition: polyglot_error.h:57
#define F
Definition: jit_executor_value.cc:374
constexpr bool unlikely(bool expr)
Definition: my_compiler.h:58
shcore::polyglot::Polyglot_error Polyglot_error
Definition: jit_executor_javascript.h:53
const char * k_key_message
Definition: polyglot_error.h:43
void throw_if_error(F f, poly_thread thread, Args &&...args)
Definition: polyglot_error.h:116
Definition: file_system_exceptions.h:34
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:430