Skip to content
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
12 changes: 9 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,12 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
sqlite3_result_null(cx);
}
};
var setFunctionError = function setFunctionError(cx, error) {
// sqlite3_result_error takes a string: passing an Error object
// directly would report an empty message
var message = error instanceof Error ? error.message : String(error);
sqlite3_result_error(cx, message, -1);
};

/** Register a custom function with SQLite
@example <caption>Register a simple function</caption>
Expand All @@ -1266,7 +1272,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
try {
result = func.apply(null, args);
} catch (error) {
sqlite3_result_error(cx, error, -1);
setFunctionError(cx, error);
return;
}
setFunctionResult(cx, result);
Expand Down Expand Up @@ -1367,7 +1373,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
state[p] = step.apply(null, mergedArgs);
} catch (error) {
delete state[p];
sqlite3_result_error(cx, error, -1);
setFunctionError(cx, error);
}
}

Expand All @@ -1378,7 +1384,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
result = finalize(state[p]);
} catch (error) {
delete state[p];
sqlite3_result_error(cx, error, -1);
setFunctionError(cx, error);
return;
}
setFunctionResult(cx, result);
Expand Down
19 changes: 19 additions & 0 deletions test/test_aggregate_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ exports.test = function (SQL, assert) {
3,
"Aggregate functions work after an exception has been thrown in finalize"
);

db.create_aggregate("throws_error_step", {
step: (state, value) => { throw new Error("step error object"); },
});
assert.throws(
() => db.exec("SELECT throws_error_step(column1) FROM (VALUES (1));"),
/step error object/,
"Reports the message of an Error thrown in a step function"
);

db.create_aggregate("throws_error_finalize", {
step: (state, value) => value,
finalize: (state) => { throw new Error("finalize error object"); },
});
assert.throws(
() => db.exec("SELECT throws_error_finalize(column1) FROM (VALUES (1));"),
/finalize error object/,
"Reports the message of an Error thrown in a finalize function"
);
}

// helper function to calculate a percentile from an array. Will modify the
Expand Down
6 changes: 5 additions & 1 deletion test/test_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ exports.test = function(SQL, assert){

db.create_function("throwFunction", function () { throw "internal exception"; return 5;} );
assert.throws(function(){db.exec("SELECT throwFunction()");},/internal exception/, "Can handle internal exceptions");


db.create_function("throwErrorFunction", function () { throw new Error("internal error object"); });
assert.throws(function(){db.exec("SELECT throwErrorFunction()");},/internal error object/, "Reports the message of a thrown Error");
assert.equal(db.exec("SELECT addOne(1);")[0]["values"][0][0], 2, "Functions work after an Error has been thrown");

db.create_function("customeObjectFunction", function () { return {test:123};} );
assert.throws(function(){db.exec("SELECT customeObjectFunction()");},/Wrong API use/, "Reports wrong API use");

Expand Down