Skip to content

Commit d628a61

Browse files
committed
src,sqlite: replace tolocalchecked with tolocal
1 parent ce72fcc commit d628a61

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/node_sqlite.cc

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,21 +1763,25 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
17631763

17641764
Local<Function> filterFunc = filterValue.As<Function>();
17651765

1766-
context.filterCallback = [env,
1767-
filterFunc](std::string_view item) -> bool {
1768-
// TODO(@jasnell): The use of ToLocalChecked here means that if
1769-
// the filter function throws an error the process will crash.
1770-
// The filterCallback should be updated to avoid the check and
1771-
// propagate the error correctly.
1772-
Local<Value> argv[] = {
1773-
String::NewFromUtf8(env->isolate(),
1774-
item.data(),
1775-
NewStringType::kNormal,
1776-
static_cast<int>(item.size()))
1777-
.ToLocalChecked()};
1778-
Local<Value> result =
1779-
filterFunc->Call(env->context(), Null(env->isolate()), 1, argv)
1780-
.ToLocalChecked();
1766+
context.filterCallback =
1767+
[db, env, filterFunc](std::string_view item) -> bool {
1768+
Local<Value> argv[1];
1769+
if (!String::NewFromUtf8(env->isolate(),
1770+
item.data(),
1771+
NewStringType::kNormal,
1772+
static_cast<int>(item.size()))
1773+
.ToLocal(&argv[0])) {
1774+
db->SetIgnoreNextSQLiteError(true);
1775+
return false;
1776+
}
1777+
1778+
Local<Value> result;
1779+
if (!filterFunc->Call(env->context(), Null(env->isolate()), 1, argv)
1780+
.ToLocal(&result)) {
1781+
db->SetIgnoreNextSQLiteError(true);
1782+
return false;
1783+
}
1784+
17811785
return result->BooleanValue(env->isolate());
17821786
};
17831787
}
@@ -2239,9 +2243,11 @@ Local<Value> StatementExecutionHelper::Get(Environment* env,
22392243
LocalVector<Name> keys(isolate);
22402244
keys.reserve(num_cols);
22412245
for (int i = 0; i < num_cols; ++i) {
2242-
MaybeLocal<Name> key = ColumnNameToName(env, stmt, i);
2243-
if (key.IsEmpty()) return Undefined(isolate);
2244-
keys.emplace_back(key.ToLocalChecked());
2246+
Local<Name> key;
2247+
if (!ColumnNameToName(env, stmt, i).ToLocal(&key)) {
2248+
return Undefined(isolate);
2249+
}
2250+
keys.emplace_back(key);
22452251
}
22462252

22472253
DCHECK_EQ(keys.size(), row_values.size());
@@ -2755,12 +2761,8 @@ BaseObjectPtr<StatementSync> SQLTagStore::PrepareStatement(
27552761

27562762
if (stmt == nullptr) {
27572763
sqlite3_stmt* s = nullptr;
2758-
Local<String> sql_str =
2759-
String::NewFromUtf8(isolate, sql.c_str()).ToLocalChecked();
2760-
Utf8Value sql_utf8(isolate, sql_str);
2761-
27622764
int r = sqlite3_prepare_v2(
2763-
session->database_->connection_, *sql_utf8, -1, &s, 0);
2765+
session->database_->connection_, sql.c_str(), -1, &s, 0);
27642766

27652767
if (r != SQLITE_OK) {
27662768
THROW_ERR_SQLITE_ERROR(isolate, "Failed to prepare statement");

test/parallel/test-sqlite-session.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,30 @@ suite('conflict resolution', () => {
366366
});
367367
});
368368

369+
test('filter handler throws', (t) => {
370+
const database1 = new DatabaseSync(':memory:');
371+
const database2 = new DatabaseSync(':memory:');
372+
const createTableSql = 'CREATE TABLE data1(key INTEGER PRIMARY KEY); CREATE TABLE data2(key INTEGER PRIMARY KEY);';
373+
database1.exec(createTableSql);
374+
database2.exec(createTableSql);
375+
376+
const session = database1.createSession();
377+
378+
database1.exec('INSERT INTO data1 (key) VALUES (1), (2), (3)');
379+
database1.exec('INSERT INTO data2 (key) VALUES (1), (2), (3), (4), (5)');
380+
381+
t.assert.throws(() => {
382+
database2.applyChangeset(session.changeset(), {
383+
filter: () => {
384+
throw new Error('some error');
385+
}
386+
});
387+
}, {
388+
name: 'Error',
389+
message: 'some error'
390+
});
391+
});
392+
369393
test('database.createSession() - filter changes', (t) => {
370394
const database1 = new DatabaseSync(':memory:');
371395
const database2 = new DatabaseSync(':memory:');

0 commit comments

Comments
 (0)