1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/preserved_error.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/exception.hpp" |
12 | #include "duckdb/common/string.hpp" |
13 | |
14 | namespace duckdb { |
15 | |
16 | class PreservedError { |
17 | public: |
18 | //! Not initialized, default constructor |
19 | DUCKDB_API PreservedError(); |
20 | //! From std::exception |
21 | PreservedError(const std::exception &ex) |
22 | : initialized(true), type(ExceptionType::INVALID), raw_message(SanitizeErrorMessage(error: ex.what())), |
23 | exception_instance(nullptr) { |
24 | } |
25 | //! From a raw string |
26 | DUCKDB_API explicit PreservedError(const string &raw_message); |
27 | //! From an Exception |
28 | DUCKDB_API PreservedError(const Exception &exception); |
29 | |
30 | public: |
31 | //! Throw the error |
32 | [[noreturn]] DUCKDB_API void Throw(const string &prepended_message = "" ) const; |
33 | //! Get the internal exception type of the error |
34 | DUCKDB_API const ExceptionType &Type() const; |
35 | //! Allows adding addition information to the message |
36 | DUCKDB_API PreservedError &AddToMessage(const string &prepended_message); |
37 | //! Used in clients like C-API, creates the final message and returns a reference to it |
38 | DUCKDB_API const string &Message(); |
39 | //! Let's us do things like 'if (error)' |
40 | DUCKDB_API operator bool() const; |
41 | DUCKDB_API bool operator==(const PreservedError &other) const; |
42 | const shared_ptr<Exception> &GetError() { |
43 | return exception_instance; |
44 | } |
45 | |
46 | private: |
47 | //! Whether this PreservedError contains an exception or not |
48 | bool initialized; |
49 | //! The ExceptionType of the preserved exception |
50 | ExceptionType type; |
51 | //! The message the exception was constructed with (does not contain the Exception Type) |
52 | string raw_message; |
53 | //! The final message (stored in the preserved error for compatibility reasons with C-API) |
54 | string final_message; |
55 | std::shared_ptr<Exception> exception_instance; |
56 | |
57 | private: |
58 | DUCKDB_API static string SanitizeErrorMessage(string error); |
59 | }; |
60 | |
61 | } // namespace duckdb |
62 | |