From 669798b209bd78facfa719bb942734f3b1ae651e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 29 Oct 2024 10:00:12 -0400 Subject: [PATCH] fix(model): avoid unhandled error if `createIndex()` throws a sync error --- lib/model.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 3f242e7f81..597c70c6c9 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1653,7 +1653,24 @@ function _ensureIndexes(model, options, callback) { } } - model.collection.createIndex(indexFields, indexOptions).then( + // Just in case `createIndex()` throws a sync error + let promise = null; + try { + promise = model.collection.createIndex(indexFields, indexOptions); + } catch (err) { + if (!indexError) { + indexError = err; + } + if (!model.$caught) { + model.emit('error', err); + } + + indexSingleDone(err, indexFields, indexOptions); + create(); + return; + } + + promise.then( name => { indexSingleDone(null, indexFields, indexOptions, name); create();