Skip to content

Commit 6f5a68d

Browse files
Giorgiclaude
andcommitted
Use duckdb_appender_error_data for appender error handling
Switch the appender from duckdb_appender_error (plain string) to duckdb_appender_error_data, which exposes both the message and the DuckDB error type. Route all appender error sites through the existing DuckDBErrorData.ThrowOnError extension (message prefix now optional), so appender exceptions carry ErrorType, consistent with the Arrow paths. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LWKN1SKSTFDyeqSrA4dfYT
1 parent 87da4c7 commit 6f5a68d

5 files changed

Lines changed: 10 additions & 20 deletions

File tree

DuckDB.NET.Bindings/NativeMethods/NativeMethods.Appender.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ public static partial class Appender
2323
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
2424
public static partial DuckDBLogicalType DuckDBAppenderColumnType(DuckDBAppender appender, ulong index);
2525

26-
[SuppressGCTransition]
27-
[LibraryImport(DuckDbLibrary, EntryPoint = "duckdb_appender_error")]
26+
[LibraryImport(DuckDbLibrary, EntryPoint = "duckdb_appender_error_data")]
2827
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
29-
[return: MarshalUsing(typeof(DuckDBOwnedStringMarshaller))]
30-
public static partial string DuckDBAppenderError(DuckDBAppender appender);
28+
public static partial DuckDBErrorData DuckDBAppenderErrorData(DuckDBAppender appender);
3129

3230
[LibraryImport(DuckDbLibrary, EntryPoint = "duckdb_appender_flush")]
3331
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]

DuckDB.NET.Data/DuckDBAppender.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DuckDB.NET.Data.Common;
22
using DuckDB.NET.Data.DataChunk.Writer;
3+
using DuckDB.NET.Data.Extensions;
34
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56

@@ -72,7 +73,7 @@ public void Clear()
7273
var state = NativeMethods.Appender.DuckDBAppenderClear(nativeAppender);
7374
if (!state.IsSuccess())
7475
{
75-
ThrowLastError(nativeAppender);
76+
NativeMethods.Appender.DuckDBAppenderErrorData(nativeAppender).ThrowOnError();
7677
}
7778

7879
rowCount = 0;
@@ -102,7 +103,7 @@ public void Close()
102103
var state = NativeMethods.Appender.DuckDBAppenderClose(nativeAppender);
103104
if (!state.IsSuccess())
104105
{
105-
ThrowLastError(nativeAppender);
106+
NativeMethods.Appender.DuckDBAppenderErrorData(nativeAppender).ThrowOnError();
106107
}
107108
}
108109
finally
@@ -137,18 +138,9 @@ private void AppendDataChunk()
137138

138139
if (!state.IsSuccess())
139140
{
140-
ThrowLastError(nativeAppender);
141+
NativeMethods.Appender.DuckDBAppenderErrorData(nativeAppender).ThrowOnError();
141142
}
142143

143144
NativeMethods.DataChunks.DuckDBDataChunkReset(dataChunk);
144145
}
145-
146-
[DoesNotReturn]
147-
[StackTraceHidden]
148-
internal static void ThrowLastError(Native.DuckDBAppender appender)
149-
{
150-
var errorMessage = NativeMethods.Appender.DuckDBAppenderError(appender);
151-
152-
throw new DuckDBException(errorMessage);
153-
}
154146
}

DuckDB.NET.Data/DuckDBAppenderRow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public IDuckDBAppenderRow AppendDefault()
135135

136136
if (state == DuckDBState.Error)
137137
{
138-
DuckDBAppender.ThrowLastError(nativeAppender);
138+
NativeMethods.Appender.DuckDBAppenderErrorData(nativeAppender).ThrowOnError();
139139
}
140140

141141
columnIndex++;

DuckDB.NET.Data/DuckDBConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public DuckDBAppender CreateAppender(string? catalog, string? schema, string tab
161161
{
162162
try
163163
{
164-
DuckDBAppender.ThrowLastError(nativeAppender);
164+
NativeMethods.Appender.DuckDBAppenderErrorData(nativeAppender).ThrowOnError();
165165
}
166166
finally
167167
{

DuckDB.NET.Data/Extensions/DuckDBErrorDataExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ namespace DuckDB.NET.Data.Extensions;
22

33
internal static class DuckDBErrorDataExtensions
44
{
5-
public static void ThrowOnError(this DuckDBErrorData errorData, string message)
5+
public static void ThrowOnError(this DuckDBErrorData errorData, string message = "")
66
{
77
using (errorData)
88
{
99
if (errorData.HasError)
1010
{
1111
var errorType = NativeMethods.ErrorData.DuckDBErrorDataErrorType(errorData);
12-
throw new DuckDBException($"{message} {errorData.Message}".TrimEnd(), errorType);
12+
throw new DuckDBException($"{message} {errorData.Message}".Trim(), errorType);
1313
}
1414
}
1515
}

0 commit comments

Comments
 (0)