From 42d190898463ad2b407151ebc05156850fd29591 Mon Sep 17 00:00:00 2001 From: tania <44262898+taniabogatsch@users.noreply.github.com> Date: Fri, 30 Aug 2024 09:33:05 +0200 Subject: [PATCH] flush before close to obtain a meaningful error message before destroying the appender object (#270) --- appender.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/appender.go b/appender.go index 0b3d5b77..31396201 100644 --- a/appender.go +++ b/appender.go @@ -8,6 +8,7 @@ import "C" import ( "database/sql/driver" + "errors" "unsafe" ) @@ -111,15 +112,26 @@ func (a *Appender) Close() error { a.closed = true // Append all remaining chunks. - err := a.appendDataChunks() + errAppend := a.appendDataChunks() - // Destroy all appender data. + // We flush before closing to get a meaningful error message. + var errFlush error + state := C.duckdb_appender_flush(a.duckdbAppender) + if state == C.DuckDBError { + errFlush = duckdbError(C.duckdb_appender_error(a.duckdbAppender)) + } + + // Destroy all appender data and the appender. destroyTypeSlice(a.ptr, a.types) - state := C.duckdb_appender_destroy(&a.duckdbAppender) + var errClose error + state = C.duckdb_appender_destroy(&a.duckdbAppender) + if state == C.DuckDBError { + errClose = errAppenderClose + } - if err != nil || state == C.DuckDBError { - // We destroyed the appender, so we cannot retrieve the duckdb internal error. - return getError(errAppenderClose, invalidatedAppenderError(err)) + err := errors.Join(errAppend, errFlush, errClose) + if err != nil { + return getError(invalidatedAppenderError(err), nil) } return nil }