Skip to content

Commit 9d8677b

Browse files
cjihrigaduh95
authored andcommitted
sqlite: handle thrown errors in result callback
This commit updates the aggregate function 'result' callback handling to not crash when an exception is thrown. Fixes: #58425 PR-URL: #58426 Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent d3d662a commit 9d8677b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/node_sqlite.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ class CustomAggregate {
373373
result = Local<Value>::New(isolate, agg->value);
374374
}
375375

376-
JSValueToSQLiteResult(isolate, ctx, result);
376+
if (!result.IsEmpty()) {
377+
JSValueToSQLiteResult(isolate, ctx, result);
378+
}
379+
377380
if (is_final) {
378381
DestroyAggregateData(ctx);
379382
}

test/parallel/test-sqlite-aggregate-function.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,27 @@ describe('step', () => {
308308
});
309309

310310
describe('result', () => {
311+
test('throws if result throws an error', (t) => {
312+
const db = new DatabaseSync(':memory:');
313+
t.after(() => db.close());
314+
db.exec('CREATE TABLE data (value INTEGER)');
315+
db.exec('INSERT INTO data VALUES (1), (2), (3)');
316+
db.aggregate('sum_int', {
317+
start: 0,
318+
step: (acc, value) => {
319+
return acc + value;
320+
},
321+
result: () => {
322+
throw new Error('result error');
323+
},
324+
});
325+
t.assert.throws(() => {
326+
db.prepare('SELECT sum_int(value) as result FROM data').get();
327+
}, {
328+
message: 'result error'
329+
});
330+
});
331+
311332
test('executes once when options.inverse is not present', (t) => {
312333
const db = new DatabaseSync(':memory:');
313334
t.after(() => db.close());

0 commit comments

Comments
 (0)
close