forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy patherrors.go
More file actions
317 lines (273 loc) · 15.3 KB
/
errors.go
File metadata and controls
317 lines (273 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package duckdb
import (
"errors"
"fmt"
"strings"
"github.com/duckdb/duckdb-go/v2/mapping"
)
func getError(errDriver, err error) error {
if err == nil {
return fmt.Errorf("%s: %w", driverErrMsg, errDriver)
}
return fmt.Errorf("%s: %w: %s", driverErrMsg, errDriver, err.Error())
}
func castError(actual, expected string) error {
return fmt.Errorf("%s: cannot cast %s to %s", castErrMsg, actual, expected)
}
func conversionError(actual, min, max int) error {
return fmt.Errorf("%s: cannot convert %d, minimum: %d, maximum: %d", convertErrMsg, actual, min, max)
}
func invalidInputError(actual, expected string) error {
return fmt.Errorf("%s: expected %s, got %s", invalidInputErrMsg, expected, actual)
}
func structFieldError(actual, expected string) error {
return fmt.Errorf("%s: expected %s, got %s", structFieldErrMsg, expected, actual)
}
func columnCountError(actual, expected int) error {
return fmt.Errorf("%s: expected %d, got %d", columnCountErrMsg, expected, actual)
}
func setValueError(colIdx, rowIdx int, val any, err error) error {
return fmt.Errorf("%s: at row %d, col %d, val: %v: %w", setValueErrMsg, rowIdx, colIdx, val, err)
}
func paramIndexError(idx int, max uint64) error {
return fmt.Errorf("%s: %d is out of range [1, %d]", paramIndexErrMsg, idx, max)
}
func columnIndexError(idx int, max uint64) error {
return fmt.Errorf("%s: %d is out of range [0, %d)", columnIndexErrMsg, idx, max)
}
func unsupportedTypeError(name string) error {
return fmt.Errorf("%s: %s", unsupportedTypeErrMsg, name)
}
func invalidatedAppenderError(err error) error {
if err == nil {
return errors.New(invalidatedAppenderMsg)
}
return fmt.Errorf("%w: %s", err, invalidatedAppenderMsg)
}
func tryOtherFuncError(hint string) error {
return fmt.Errorf("%s: %s", tryOtherFuncErrMsg, hint)
}
func addIndexToError(err error, idx int) error {
return fmt.Errorf("%w: %s: %d", err, indexErrMsg, idx)
}
func interfaceIsNilError(interfaceName string) error {
return fmt.Errorf("%s: %s", interfaceIsNilErrMsg, interfaceName)
}
func duplicateNameError(name string) error {
return fmt.Errorf("%s: %s", duplicateNameErrMsg, name)
}
const (
driverErrMsg = "database/sql/driver"
castErrMsg = "cast error"
convertErrMsg = "conversion error"
invalidInputErrMsg = "invalid input"
structFieldErrMsg = "invalid STRUCT field"
columnCountErrMsg = "invalid column count"
setValueErrMsg = "failed to set value"
unprojectedColumnErrMsg = "unprojected column"
unsupportedTypeErrMsg = "unsupported data type"
invalidatedAppenderMsg = "appended and not yet flushed data has been invalidated due to error"
tryOtherFuncErrMsg = "please try this function instead"
indexErrMsg = "index"
unknownTypeErrMsg = "unknown type"
interfaceIsNilErrMsg = "interface is nil"
duplicateNameErrMsg = "duplicate name"
paramIndexErrMsg = "invalid parameter index"
columnIndexErrMsg = "invalid column index"
)
var (
errInternal = errors.New("internal error: please file a bug report at duckdb-go")
errAPI = errors.New("API error")
errVectorSize = errors.New("data chunks cannot exceed duckdb's internal vector size")
errConnect = errors.New("could not connect to database")
errParseDSN = errors.New("could not parse DSN for database")
errSetConfig = errors.New("could not set invalid or local option for global database config")
errCreateConfig = errors.New("could not create config for database")
errInvalidCon = errors.New("not a DuckDB driver connection")
errClosedCon = errors.New("closed connection")
errClosedStmt = errors.New("closed statement")
errUninitializedStmt = errors.New("uninitialized statement")
errPrepare = errors.New("could not prepare query")
errMissingPrepareContext = errors.New("missing context for multi-statement query: try using PrepareContext")
errEmptyQuery = errors.New("empty query")
errCouldNotBind = errors.New("could not bind parameter")
errActiveRows = errors.New("ExecContext or QueryContext with active Rows")
errNotBound = errors.New("parameters have not been bound")
errBeginTx = errors.New("could not begin transaction")
errMultipleTx = errors.New("multiple transactions")
errReadOnlyTxNotSupported = errors.New("read-only transactions are not supported")
errIsolationLevelNotSupported = errors.New("isolation level not supported: duckdb-go only supports the default isolation level")
errAppenderCreation = errors.New("could not create appender")
errAppenderClose = errors.New("could not close appender")
errAppenderDoubleClose = fmt.Errorf("%w: already closed", errAppenderClose)
errAppenderAppendRow = errors.New("could not append row")
errAppenderAppendAfterClose = fmt.Errorf("%w: appender already closed", errAppenderAppendRow)
errAppenderFlush = errors.New("could not flush appender")
errAppenderEmptyQuery = errors.New("empty query")
errAppenderEmptyColumnTypes = errors.New("empty column types")
errAppenderColumnMismatch = errors.New("mismatch between the number of column types and names")
errUnsupportedMapKeyType = errors.New("MAP key type not supported")
errEmptyName = errors.New("empty name")
errInvalidDecimalWidth = fmt.Errorf("the DECIMAL with must be between 1 and %d", max_decimal_width)
errInvalidDecimalScale = errors.New("the DECIMAL scale must be less than or equal to the width")
errInvalidArraySize = errors.New("invalid ARRAY size")
errSetSQLNULLValue = errors.New("cannot write to a NULL column")
errScalarUDFCreate = errors.New("could not create scalar UDF")
errScalarUDFNoName = fmt.Errorf("%w: missing name", errScalarUDFCreate)
errScalarUDFIsNil = fmt.Errorf("%w: function is nil", errScalarUDFCreate)
errScalarUDFNoExecutor = fmt.Errorf("%w: executor is nil", errScalarUDFCreate)
errScalarUDFInputTypeIsNil = fmt.Errorf("%w: input type is nil", errScalarUDFCreate)
errScalarUDFResultTypeIsNil = fmt.Errorf("%w: result type is nil", errScalarUDFCreate)
errScalarUDFResultTypeIsANY = fmt.Errorf("%w: result type is ANY, which is not supported", errScalarUDFCreate)
errScalarUDFCreateSet = fmt.Errorf("could not create scalar UDF set")
errScalarUDFAddToSet = fmt.Errorf("%w: could not add the function to the set", errScalarUDFCreateSet)
errTableUDFCreate = errors.New("could not create table UDF")
errTableUDFNoName = fmt.Errorf("%w: missing name", errTableUDFCreate)
errTableUDFMissingBindArgs = fmt.Errorf("%w: missing bind arguments", errTableUDFCreate)
errTableUDFArgumentIsNil = fmt.Errorf("%w: argument is nil", errTableUDFCreate)
errTableUDFColumnTypeIsNil = fmt.Errorf("%w: column type is nil", errTableUDFCreate)
errProfilingInfoEmpty = errors.New("no profiling information available for this connection")
)
type ErrorType int
const (
ErrorTypeInvalid = ErrorType(mapping.ErrorTypeInvalid) // Invalid type.
ErrorTypeOutOfRange = ErrorType(mapping.ErrorTypeOutOfRange) // The type's value is out of range.
ErrorTypeConversion = ErrorType(mapping.ErrorTypeConversion) // Conversion/casting error.
ErrorTypeUnknownType = ErrorType(mapping.ErrorTypeUnknownType) // The type is unknown.
ErrorTypeDecimal = ErrorType(mapping.TypeDecimal) // Decimal-related error.
ErrorTypeMismatchType = ErrorType(mapping.ErrorTypeMismatchType) // Types don't match.
ErrorTypeDivideByZero = ErrorType(mapping.ErrorTypeDivideByZero) // Division by zero.
ErrorTypeObjectSize = ErrorType(mapping.ErrorTypeObjectSize) // Exceeds object size.
ErrorTypeInvalidType = ErrorType(mapping.ErrorTypeInvalidType) // Incompatible types.
ErrorTypeSerialization = ErrorType(mapping.ErrorTypeSerialization) // Type serialization error.
ErrorTypeTransaction = ErrorType(mapping.ErrorTypeTransaction) // Transaction conflict.
ErrorTypeNotImplemented = ErrorType(mapping.ErrorTypeNotImplemented) // Missing functionality.
ErrorTypeExpression = ErrorType(mapping.ErrorTypeExpression) // Expression error.
ErrorTypeCatalog = ErrorType(mapping.ErrorTypeCatalog) // Catalog error.
ErrorTypeParser = ErrorType(mapping.ErrorTypeParser) // Error during parsing.
ErrorTypePlanner = ErrorType(mapping.ErrorTypePlanner) // Error during planning.
ErrorTypeScheduler = ErrorType(mapping.ErrorTypeScheduler) // Scheduling error.
ErrorTypeExecutor = ErrorType(mapping.ErrorTypeExecutor) // Executor error.
ErrorTypeConstraint = ErrorType(mapping.ErrorTypeConstraint) // Constraint violation.
ErrorTypeIndex = ErrorType(mapping.ErrorTypeIndex) // Index error.
ErrorTypeStat = ErrorType(mapping.ErrorTypeStat) // Statistics error.
ErrorTypeConnection = ErrorType(mapping.ErrorTypeConnection) // Connection error.
ErrorTypeSyntax = ErrorType(mapping.ErrorTypeSyntax) // Invalid syntax.
ErrorTypeSettings = ErrorType(mapping.ErrorTypeSettings) // Settings-related error.
ErrorTypeBinder = ErrorType(mapping.ErrorTypeBinder) // Binding error.
ErrorTypeNetwork = ErrorType(mapping.ErrorTypeNetwork) // Network error.
ErrorTypeOptimizer = ErrorType(mapping.ErrorTypeOptimizer) // Optimizer error.
ErrorTypeNullPointer = ErrorType(mapping.ErrorTypeNullPointer) // Null-pointer exception.
ErrorTypeIO = ErrorType(mapping.ErrorTypeErrorIO) // IO exception.
ErrorTypeInterrupt = ErrorType(mapping.ErrorTypeInterrupt) // Query interruption.
ErrorTypeFatal = ErrorType(mapping.ErrorTypeFatal) // Fatal exception. Non-recoverable. The DB enters an invalid state and must be restarted.
ErrorTypeInternal = ErrorType(mapping.ErrorTypeInternal) // Internal exception. Indicates a bug, and should be reported.
ErrorTypeInvalidInput = ErrorType(mapping.ErrorTypeInvalidInput) // Invalid input.
ErrorTypeOutOfMemory = ErrorType(mapping.ErrorTypeOutOfMemory) // Out-of-memory error.
ErrorTypePermission = ErrorType(mapping.ErrorTypePermission) // Invalid permissions.
ErrorTypeParameterNotResolved = ErrorType(mapping.ErrorTypeParameterNotResolved) // Error when resolving types.
ErrorTypeParameterNotAllowed = ErrorType(mapping.ErrorTypeParameterNotAllowed) // Invalid parameter.
ErrorTypeDependency = ErrorType(mapping.ErrorTypeDependency) // Dependency error.
ErrorTypeHTTP = ErrorType(mapping.ErrorTypeHTTP) // HTTP error.
ErrorTypeMissingExtension = ErrorType(mapping.ErrorTypeMissingExtension) // Usage of a non-loaded extension.
ErrorTypeAutoLoad = ErrorType(mapping.ErrorTypeAutoload) // Usage of a non-loaded extension that cannot be loaded automatically.
ErrorTypeSequence = ErrorType(mapping.ErrorTypeSequence) // Sequence error.
ErrorTypeInvalidConfiguration = ErrorType(mapping.ErrorTypeInvalidConfiguration) // Indicates an invalid configuration, e.g., a missing Secret parameter, or a mandatory setting is not provided.
)
var errorPrefixMap = map[string]ErrorType{
"Invalid Error": ErrorTypeInvalid,
"Out of Range Error": ErrorTypeOutOfRange,
"Conversion Error": ErrorTypeConversion,
"Error": ErrorTypeUnknownType,
"Decimal Error": ErrorTypeDecimal,
"Mismatch Type Error": ErrorTypeMismatchType,
"Divide by Zero Error": ErrorTypeDivideByZero,
"Object Size Error": ErrorTypeObjectSize,
"Invalid type Error": ErrorTypeInvalidType,
"Serialization Error": ErrorTypeSerialization,
"TransactionContext Error": ErrorTypeTransaction,
"Not implemented Error": ErrorTypeNotImplemented,
"Expression Error": ErrorTypeExpression,
"Catalog Error": ErrorTypeCatalog,
"Parser Error": ErrorTypeParser,
"Planner Error": ErrorTypePlanner,
"Scheduler Error": ErrorTypeScheduler,
"Executor Error": ErrorTypeExecutor,
"Constraint Error": ErrorTypeConstraint,
"Index Error": ErrorTypeIndex,
"Stat Error": ErrorTypeStat,
"Connection Error": ErrorTypeConnection,
"Syntax Error": ErrorTypeSyntax,
"Settings Error": ErrorTypeSettings,
"Binder Error": ErrorTypeBinder,
"Network Error": ErrorTypeNetwork,
"Optimizer Error": ErrorTypeOptimizer,
"NullPointer Error": ErrorTypeNullPointer,
"IO Error": ErrorTypeIO,
"INTERRUPT Error": ErrorTypeInterrupt,
"FATAL Error": ErrorTypeFatal,
"INTERNAL Error": ErrorTypeInternal,
"Invalid Input Error": ErrorTypeInvalidInput,
"Out of Memory Error": ErrorTypeOutOfMemory,
"Permission Error": ErrorTypePermission,
"Parameter Not Resolved Error": ErrorTypeParameterNotResolved,
"Parameter Not Allowed Error": ErrorTypeParameterNotAllowed,
"Dependency Error": ErrorTypeDependency,
"HTTP Error": ErrorTypeHTTP,
"Missing Extension Error": ErrorTypeMissingExtension,
"Extension Autoloading Error": ErrorTypeAutoLoad,
"Sequence Error": ErrorTypeSequence,
"Invalid Configuration Error": ErrorTypeInvalidConfiguration,
}
type Error struct {
Type ErrorType
Msg string
}
func (e *Error) Error() string {
return e.Msg
}
func (e *Error) Is(err error) bool {
if other, ok := err.(*Error); ok {
return other.Msg == e.Msg
}
return false
}
func errorDataError(errorData mapping.ErrorData) error {
defer mapping.DestroyErrorData(&errorData)
if !mapping.ErrorDataHasError(errorData) {
return nil
}
t := mapping.ErrorDataErrorType(errorData)
msg := mapping.ErrorDataMessage(errorData)
return &Error{ErrorType(t), msg}
}
func getDuckDBError(errMsg string) error {
errType := ErrorTypeInvalid
// Find the end of the prefix ("<error-type> Error: ").
if prefix, _, ok := strings.Cut(errMsg, ": "); ok {
if typ, ok := errorPrefixMap[prefix]; ok {
errType = typ
}
}
return &Error{
Type: errType,
Msg: errMsg,
}
}
type unprojectedColumnError struct {
Index int
}
func (e *unprojectedColumnError) Error() string {
return fmt.Sprintf("unprojected column: index %d is not projected", e.Index)
}
// sentinel value for errors.Is
var errUnprojectedColumn = &unprojectedColumnError{Index: -1}
// constructor that wraps the sentinel with context
func newUnprojectedColumnError(index int) error {
if index == -1 {
// avoid returning the sentinel directly for real index -1
return &unprojectedColumnError{Index: index}
}
// wrap the sentinel so errors.Is works, but keep the index in the concrete error
return fmt.Errorf("%w: index %d", errUnprojectedColumn, index)
}