Skip to content

Commit 482bc3a

Browse files
committed
Fix: JS: Improve error handling of batch addition
Crash if there is an error in batch addition. Example: ``` const usearch = require('usearch'); const index = new usearch.Index({ metric: 'l2sq', connectivity: 16, dimensions: 3 }); index.add(42n, new Float32Array([0.2, 0.6, 0.4])); index.add( [41n, 42n, 43n], [[0.1, 0.6, 0.4], [0.2, 0.6, 0.4], [0.3, 0.6, 0.4]] ) ``` `42n` is duplicated, which causes an error, but then it crash. It seems that it is not better to throw an exception during the process, so we changed it to throw it after it is completed.
1 parent 8d8a916 commit 482bc3a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

javascript/lib.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,16 @@ void CompiledIndex::Add(Napi::CallbackInfo const& ctx) {
165165

166166
// Create an instance of the executor with the default number of threads
167167
auto run_parallel = [&](auto vectors) {
168+
std::string error = "";
168169
executor_stl_t executor;
169170
executor.fixed(tasks, [&](std::size_t /*thread_idx*/, std::size_t task_idx) {
170171
add_result_t result = native_->add(static_cast<default_key_t>(keys[task_idx]),
171172
vectors + task_idx * native_->dimensions());
172173
if (!result)
173-
Napi::Error::New(ctx.Env(), result.error.release()).ThrowAsJavaScriptException();
174+
error += "<key:" + std::to_string(keys[task_idx]) + " message:" + result.error.release() + ">";
174175
});
176+
if (error.size() > 0)
177+
Napi::Error::New(ctx.Env(), error).ThrowAsJavaScriptException();
175178
};
176179

177180
Napi::TypedArray vectors = ctx[1].As<Napi::TypedArray>();

0 commit comments

Comments
 (0)