Skip to content

Commit 8e4411d

Browse files
authored
Merge pull request Automattic#14995 from Automattic/vkarpov15/createindex-sync-error
fix(model): avoid unhandled error if `createIndex()` throws a sync error
2 parents 36e3d9d + 669798b commit 8e4411d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/model.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,24 @@ function _ensureIndexes(model, options, callback) {
16541654
}
16551655
}
16561656

1657-
model.collection.createIndex(indexFields, indexOptions).then(
1657+
// Just in case `createIndex()` throws a sync error
1658+
let promise = null;
1659+
try {
1660+
promise = model.collection.createIndex(indexFields, indexOptions);
1661+
} catch (err) {
1662+
if (!indexError) {
1663+
indexError = err;
1664+
}
1665+
if (!model.$caught) {
1666+
model.emit('error', err);
1667+
}
1668+
1669+
indexSingleDone(err, indexFields, indexOptions);
1670+
create();
1671+
return;
1672+
}
1673+
1674+
promise.then(
16581675
name => {
16591676
indexSingleDone(null, indexFields, indexOptions, name);
16601677
create();

0 commit comments

Comments
 (0)