Skip to content

Commit

Permalink
sqlite: improve error message when applying changeset fails
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers committed Dec 24, 2024
1 parent 3635cd9 commit e6001b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
}
}

inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
const char* errstr = sqlite3_errstr(errcode);
Local<Object> e;

if (CreateSQLiteError(isolate, errstr).ToLocal(&e)) {
e->Set(isolate->GetCurrentContext(),
OneByteString(isolate, "errcode"),
Integer::New(isolate, errcode))
.IsNothing();
isolate->ThrowException(e);
}
}

class UserDefinedFunction {
public:
explicit UserDefinedFunction(Environment* env,
Expand Down Expand Up @@ -824,12 +837,16 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
xFilter,
xConflict,
nullptr);
if (r == SQLITE_OK) {
args.GetReturnValue().Set(true);
return;
}
if (r == SQLITE_ABORT) {
// this is not an error, return false
args.GetReturnValue().Set(false);
return;
}
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
args.GetReturnValue().Set(true);
THROW_ERR_SQLITE_ERROR(env->isolate(), r);
}

void DatabaseSync::EnableLoadExtension(
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-sqlite-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ suite('conflict resolution', () => {
t.assert.strictEqual(conflictType, constants.SQLITE_CHANGESET_CONSTRAINT);
deepStrictEqual(t)(database2.prepare('SELECT key, value from data').all(), [{ key: 2, value: 'hello' }]);
});

test("conflict resolution handler returns invalid value", (t) => {

Check failure on line 326 in test/parallel/test-sqlite-session.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
const { database2, changeset } = prepareConflict();
t.assert.throws(() => {
database2.applyChangeset(changeset, {
onConflict: () => {
return -1;
}
});
}, {
name: 'Error',
message: 'bad parameter or other API misuse'
});
});
});

test('database.createSession() - filter changes', (t) => {
Expand Down

0 comments on commit e6001b3

Please sign in to comment.