1#include "duckdb/common/exception.hpp"
2#include "duckdb/common/string_util.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7#define FORMAT_CONSTRUCTOR(msg) \
8 va_list ap; \
9 va_start(ap, msg); \
10 Format(ap); \
11 va_end(ap);
12
13Exception::Exception(string message) : std::exception(), type(ExceptionType::INVALID) {
14 exception_message_ = message;
15}
16
17Exception::Exception(ExceptionType exception_type, string message) : std::exception(), type(exception_type) {
18 exception_message_ = ExceptionTypeToString(exception_type) + ": " + message;
19}
20
21const char *Exception::what() const noexcept {
22 return exception_message_.c_str();
23}
24
25void Exception::Format(va_list ap) {
26 exception_message_ = StringUtil::VFormat(exception_message_, ap);
27}
28
29string Exception::ExceptionTypeToString(ExceptionType type) {
30 switch (type) {
31 case ExceptionType::INVALID:
32 return "Invalid";
33 case ExceptionType::OUT_OF_RANGE:
34 return "Out of Range";
35 case ExceptionType::CONVERSION:
36 return "Conversion";
37 case ExceptionType::UNKNOWN_TYPE:
38 return "Unknown Type";
39 case ExceptionType::DECIMAL:
40 return "Decimal";
41 case ExceptionType::MISMATCH_TYPE:
42 return "Mismatch Type";
43 case ExceptionType::DIVIDE_BY_ZERO:
44 return "Divide by Zero";
45 case ExceptionType::OBJECT_SIZE:
46 return "Object Size";
47 case ExceptionType::INVALID_TYPE:
48 return "Invalid type";
49 case ExceptionType::SERIALIZATION:
50 return "Serialization";
51 case ExceptionType::TRANSACTION:
52 return "TransactionContext";
53 case ExceptionType::NOT_IMPLEMENTED:
54 return "Not implemented";
55 case ExceptionType::EXPRESSION:
56 return "Expression";
57 case ExceptionType::CATALOG:
58 return "Catalog";
59 case ExceptionType::PARSER:
60 return "Parser";
61 case ExceptionType::PLANNER:
62 return "Planner";
63 case ExceptionType::SCHEDULER:
64 return "Scheduler";
65 case ExceptionType::EXECUTOR:
66 return "Executor";
67 case ExceptionType::CONSTRAINT:
68 return "Constraint";
69 case ExceptionType::INDEX:
70 return "Index";
71 case ExceptionType::STAT:
72 return "Stat";
73 case ExceptionType::CONNECTION:
74 return "Connection";
75 case ExceptionType::SYNTAX:
76 return "Syntax";
77 case ExceptionType::SETTINGS:
78 return "Settings";
79 case ExceptionType::OPTIMIZER:
80 return "Optimizer";
81 case ExceptionType::NULL_POINTER:
82 return "NullPointer";
83 case ExceptionType::IO:
84 return "IO";
85 case ExceptionType::INTERRUPT:
86 return "INTERRUPT";
87 case ExceptionType::FATAL:
88 return "FATAL";
89 case ExceptionType::INTERNAL:
90 return "INTERNAL";
91 default:
92 return "Unknown";
93 }
94}
95
96CastException::CastException(const TypeId origType, const TypeId newType)
97 : Exception(ExceptionType::CONVERSION,
98 "Type " + TypeIdToString(origType) + " can't be cast as " + TypeIdToString(newType)) {
99}
100
101ValueOutOfRangeException::ValueOutOfRangeException(const int64_t value, const TypeId origType, const TypeId newType)
102 : Exception(ExceptionType::CONVERSION, "Type " + TypeIdToString(origType) + " with value " +
103 std::to_string((intmax_t)value) +
104 " can't be cast because the value is out of range "
105 "for the destination type " +
106 TypeIdToString(newType)) {
107}
108
109ValueOutOfRangeException::ValueOutOfRangeException(const double value, const TypeId origType, const TypeId newType)
110 : Exception(ExceptionType::CONVERSION, "Type " + TypeIdToString(origType) + " with value " + std::to_string(value) +
111 " can't be cast because the value is out of range "
112 "for the destination type " +
113 TypeIdToString(newType)) {
114}
115
116ValueOutOfRangeException::ValueOutOfRangeException(const TypeId varType, const idx_t length)
117 : Exception(ExceptionType::OUT_OF_RANGE, "The value is too long to fit into type " + TypeIdToString(varType) + "(" +
118 std::to_string(length) + ")"){};
119
120ConversionException::ConversionException(string msg, ...) : Exception(ExceptionType::CONVERSION, msg) {
121 FORMAT_CONSTRUCTOR(msg);
122}
123
124InvalidTypeException::InvalidTypeException(TypeId type, string msg)
125 : Exception(ExceptionType::INVALID_TYPE, "Invalid Type [" + TypeIdToString(type) + "]: " + msg) {
126}
127
128TypeMismatchException::TypeMismatchException(const TypeId type_1, const TypeId type_2, string msg)
129 : Exception(ExceptionType::MISMATCH_TYPE,
130 "Type " + TypeIdToString(type_1) + " does not match with " + TypeIdToString(type_2) + ". " + msg) {
131}
132
133TransactionException::TransactionException(string msg, ...) : Exception(ExceptionType::TRANSACTION, msg) {
134 FORMAT_CONSTRUCTOR(msg);
135}
136
137NotImplementedException::NotImplementedException(string msg, ...) : Exception(ExceptionType::NOT_IMPLEMENTED, msg) {
138 FORMAT_CONSTRUCTOR(msg);
139}
140
141OutOfRangeException::OutOfRangeException(string msg, ...) : Exception(ExceptionType::OUT_OF_RANGE, msg) {
142 FORMAT_CONSTRUCTOR(msg);
143}
144
145CatalogException::CatalogException(string msg, ...) : StandardException(ExceptionType::CATALOG, msg) {
146 FORMAT_CONSTRUCTOR(msg);
147}
148
149ParserException::ParserException(string msg, ...) : StandardException(ExceptionType::PARSER, msg) {
150 FORMAT_CONSTRUCTOR(msg);
151}
152
153SyntaxException::SyntaxException(string msg, ...) : Exception(ExceptionType::SYNTAX, msg) {
154 FORMAT_CONSTRUCTOR(msg);
155}
156
157ConstraintException::ConstraintException(string msg, ...) : Exception(ExceptionType::CONSTRAINT, msg) {
158 FORMAT_CONSTRUCTOR(msg);
159}
160
161BinderException::BinderException(string msg, ...) : StandardException(ExceptionType::BINDER, msg) {
162 FORMAT_CONSTRUCTOR(msg);
163}
164
165IOException::IOException(string msg, ...) : Exception(ExceptionType::IO, msg) {
166 FORMAT_CONSTRUCTOR(msg);
167}
168
169SerializationException::SerializationException(string msg, ...) : Exception(ExceptionType::SERIALIZATION, msg) {
170 FORMAT_CONSTRUCTOR(msg);
171}
172
173SequenceException::SequenceException(string msg, ...) : Exception(ExceptionType::SERIALIZATION, msg) {
174 FORMAT_CONSTRUCTOR(msg);
175}
176
177InterruptException::InterruptException() : Exception(ExceptionType::INTERRUPT, "Interrupted!") {
178}
179
180FatalException::FatalException(string msg, ...) : Exception(ExceptionType::FATAL, msg) {
181 FORMAT_CONSTRUCTOR(msg);
182}
183
184InternalException::InternalException(string msg, ...) : Exception(ExceptionType::INTERNAL, msg) {
185 FORMAT_CONSTRUCTOR(msg);
186}
187