Repository navigation
Report SQL errors from db.exec result sets - #18
Conversation
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
|
Merged into 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, Two details I checked closely:
The interaction with #11 is also fine: an interrupt now surfaces as a catchable Thanks for documenting |
db.exec()prepares a statement and returns a result set, and the statement runs asnext()ortoArray()steps it. Both treat anysqlite3_step()result other thanSQLITE_ROWas the end of the rows, so an error while the statement runs is dropped.next()returnsfalse, as at the end of the rows, andtoArray()returns the rows read before the error. An error found when the statement is prepared, such as an unknown function, is already thrown bydb.exec().So a failed write looks like a successful one, and JavaScript that writes through
db.exec()cannot tell them apart.This throws an
InternalErrorwith the connection's error message whensqlite3_step()returns neitherSQLITE_ROWnorSQLITE_DONE, fromnext()and fromtoArray(). The statement is finalized as before, and the end of the rows is stillfalse. The README gains a short Running SQL from JavaScript section, sincedb.exec()was undocumented. It also says that a statement runs only whennext()ortoArray()steps it, so anINSERTneeds onenext(), which surprised us.Test: a new Exec errors test checks that:
next();toArray();INSERTare unchanged.It fails without this change.
make testpasses 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()andcheck_error()at the same place intest/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