Skip to content

Report SQL errors from db.exec result sets - #18

Merged
marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:report-db-exec-errors
Nov 12, 2026
Merged

marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:report-db-exec-errors

Conversation

@davidschachterADFA

Copy link
Copy Markdown
Contributor

db.exec() prepares a statement and returns a result set, and the statement runs as next() or toArray() steps it. Both treat any sqlite3_step() result other than SQLITE_ROW as the end of the rows, so an error while the statement runs is dropped. next() returns false, as at the end of the rows, and toArray() returns the rows read before the error. An error found when the statement is prepared, such as an unknown function, is already thrown by db.exec().

CREATE TABLE u(a UNIQUE); INSERT INTO u VALUES (1);
SELECT js_eval('db.exec("INSERT INTO u VALUES (1)").next()');               -- 0: no error, nothing inserted
SELECT js_eval('db.exec("SELECT abs(-9223372036854775808)").next()');       -- 0: integer overflow, no error
SELECT js_eval('JSON.stringify(db.exec("SELECT 1 UNION ALL SELECT abs(-9223372036854775808)").toArray())');  -- [[1]]

So a failed write looks like a successful one, and JavaScript that writes through db.exec() cannot tell them apart.

This throws an InternalError with the connection's error message when sqlite3_step() returns neither SQLITE_ROW nor SQLITE_DONE, from next() and from toArray(). The statement is finalized as before, and the end of the rows is still false. The README gains a short Running SQL from JavaScript section, since db.exec() was undocumented. It also says that a statement runs only when next() or toArray() steps it, so an INSERT needs one next(), which surprised us.

Test: a new Exec errors test checks that:

  • a failed constraint and an overflow throw from next();
  • an overflow on the second row throws from toArray();
  • JavaScript can catch the error;
  • the end of the rows and a successful INSERT are unchanged.

It fails without this change. make test passes on x86_64 Linux, also under ASan and UBSan, and the extension builds for Android (arm64 and armv7, NDK r29).

This is an eighth pull request, after #11 to #17, and independent of them. Like each of them, it adds the test helpers test_open() and check_error() at the same place in test/main.c, so whichever merges later needs a small rebase, which we will do. #17's recursion test passes with or without this change.

This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

db.exec() prepares a statement and returns a result set; the statement
runs as next() or toArray() steps it. Both treated any sqlite3_step()
result other than SQLITE_ROW as the end of the rows, so an error while
the statement runs was dropped: next() returned false, as at the end of
the rows, and toArray() returned the rows read before the error. A
failed UNIQUE constraint on an INSERT, or an integer overflow, looked
like success, and JavaScript that writes through db.exec() could not
tell. An error found when the statement is prepared, such as an unknown
function, was already thrown by db.exec().

Throw an InternalError with the connection's error message when
sqlite3_step() returns neither SQLITE_ROW nor SQLITE_DONE, from next()
and from toArray(). The statement is finalized as before. The end of
the rows is still false. The README now documents db.exec(), including
that a statement runs only when next() or toArray() steps it.

Add an Exec errors test: a failed constraint and an overflow throw from
next(), an overflow on the second row throws from toArray(), JavaScript
can catch the error, and the end of the rows and a successful INSERT
are unchanged. It fails without this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
@marcobambini

Copy link
Copy Markdown
Member

Merged into main as part of a review of #11–#19 together.

Verified locally on macOS/arm64: universal build with no new warnings, full suite green, clean under ASan + UBSan.

The new Exec errors test is load-bearing: with the source change reverted, db.exec("INSERT INTO u VALUES (1)").next() reports no error and the test fails.

Two details I checked closely:

  • sqlite3_errmsg(sqlite3_db_handle(rs->vm)) is read before sqlite3_finalize, which is the right order — the handle needs a live statement.
  • In js_rowset_to_array, the int rc hoisted out of the loop is uninitialised at declaration. It is always assigned before the rc != SQLITE_DONE check, because while (true) always executes rc = sqlite3_step(...) at least once and the only other exit is an early return. Clang with -Wall -Wextra is satisfied on both arm64 and x86_64. Correct, just worth having confirmed.

The interaction with #11 is also fine: an interrupt now surfaces as a catchable InternalError from next(), but QuickJS's interrupt handler then stops the code uncatchably at the next bytecode check, so JavaScript cannot swallow a cancellation by looping.

Thanks for documenting db.exec while you were in there — that section was overdue, and the note that a statement only runs when you step it will save people the same surprise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants