Skip to content

sqlite: handle thrown errors in result callback #58426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ class CustomAggregate {
result = Local<Value>::New(isolate, agg->value);
}

JSValueToSQLiteResult(isolate, ctx, result);
if (!result.IsEmpty()) {
JSValueToSQLiteResult(isolate, ctx, result);
}

if (is_final) {
DestroyAggregateData(ctx);
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-sqlite-aggregate-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,27 @@ describe('step', () => {
});

describe('result', () => {
test('throws if result throws an error', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => db.close());
db.exec('CREATE TABLE data (value INTEGER)');
db.exec('INSERT INTO data VALUES (1), (2), (3)');
db.aggregate('sum_int', {
start: 0,
step: (acc, value) => {
return acc + value;
},
result: () => {
throw new Error('result error');
},
});
t.assert.throws(() => {
db.prepare('SELECT sum_int(value) as result FROM data').get();
}, {
message: 'result error'
});
});

test('executes once when options.inverse is not present', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => db.close());
Expand Down
Loading