1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/main/error_manager.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/common.hpp" |
12 | #include "duckdb/common/exception.hpp" |
13 | #include "duckdb/common/map.hpp" |
14 | |
15 | namespace duckdb { |
16 | class ClientContext; |
17 | class DatabaseInstance; |
18 | |
19 | enum class ErrorType : uint16_t { |
20 | // error message types |
21 | UNSIGNED_EXTENSION = 0, |
22 | INVALIDATED_TRANSACTION = 1, |
23 | INVALIDATED_DATABASE = 2, |
24 | |
25 | // this should always be the last value |
26 | ERROR_COUNT, |
27 | INVALID = 65535, |
28 | }; |
29 | |
30 | //! The error manager class is responsible for formatting error messages |
31 | //! It allows for error messages to be overridden by extensions and clients |
32 | class ErrorManager { |
33 | public: |
34 | template <typename... Args> |
35 | string FormatException(ErrorType error_type, Args... params) { |
36 | vector<ExceptionFormatValue> values; |
37 | return FormatExceptionRecursive(error_type, values, params...); |
38 | } |
39 | |
40 | DUCKDB_API string FormatExceptionRecursive(ErrorType error_type, vector<ExceptionFormatValue> &values); |
41 | |
42 | template <class T, typename... Args> |
43 | string FormatExceptionRecursive(ErrorType error_type, vector<ExceptionFormatValue> &values, T param, |
44 | Args... params) { |
45 | values.push_back(ExceptionFormatValue::CreateFormatValue<T>(param)); |
46 | return FormatExceptionRecursive(error_type, values, params...); |
47 | } |
48 | |
49 | template <typename... Args> |
50 | static string FormatException(ClientContext &context, ErrorType error_type, Args... params) { |
51 | return Get(context).FormatException(error_type, params...); |
52 | } |
53 | |
54 | DUCKDB_API static string InvalidUnicodeError(const string &input, const string &context); |
55 | |
56 | //! Adds a custom error for a specific error type |
57 | void AddCustomError(ErrorType type, string new_error); |
58 | |
59 | DUCKDB_API static ErrorManager &Get(ClientContext &context); |
60 | DUCKDB_API static ErrorManager &Get(DatabaseInstance &context); |
61 | |
62 | private: |
63 | map<ErrorType, string> custom_errors; |
64 | }; |
65 | |
66 | } // namespace duckdb |
67 | |