Skip to content

Commit e6001b3

Browse files
committed
sqlite: improve error message when applying changeset fails
1 parent 3635cd9 commit e6001b3

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/node_sqlite.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
114114
}
115115
}
116116

117+
inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
118+
const char* errstr = sqlite3_errstr(errcode);
119+
Local<Object> e;
120+
121+
if (CreateSQLiteError(isolate, errstr).ToLocal(&e)) {
122+
e->Set(isolate->GetCurrentContext(),
123+
OneByteString(isolate, "errcode"),
124+
Integer::New(isolate, errcode))
125+
.IsNothing();
126+
isolate->ThrowException(e);
127+
}
128+
}
129+
117130
class UserDefinedFunction {
118131
public:
119132
explicit UserDefinedFunction(Environment* env,
@@ -824,12 +837,16 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
824837
xFilter,
825838
xConflict,
826839
nullptr);
840+
if (r == SQLITE_OK) {
841+
args.GetReturnValue().Set(true);
842+
return;
843+
}
827844
if (r == SQLITE_ABORT) {
845+
// this is not an error, return false
828846
args.GetReturnValue().Set(false);
829847
return;
830848
}
831-
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
832-
args.GetReturnValue().Set(true);
849+
THROW_ERR_SQLITE_ERROR(env->isolate(), r);
833850
}
834851

835852
void DatabaseSync::EnableLoadExtension(

test/parallel/test-sqlite-session.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ suite('conflict resolution', () => {
322322
t.assert.strictEqual(conflictType, constants.SQLITE_CHANGESET_CONSTRAINT);
323323
deepStrictEqual(t)(database2.prepare('SELECT key, value from data').all(), [{ key: 2, value: 'hello' }]);
324324
});
325+
326+
test("conflict resolution handler returns invalid value", (t) => {
327+
const { database2, changeset } = prepareConflict();
328+
t.assert.throws(() => {
329+
database2.applyChangeset(changeset, {
330+
onConflict: () => {
331+
return -1;
332+
}
333+
});
334+
}, {
335+
name: 'Error',
336+
message: 'bad parameter or other API misuse'
337+
});
338+
});
325339
});
326340

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

0 commit comments

Comments
 (0)