|
1 | 1 | import type { Document } from '../bson'; |
2 | 2 | import type { Collection } from '../collection'; |
3 | 3 | import type { Db } from '../db'; |
4 | | -import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoServerError } from '../error'; |
| 4 | +import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoError } from '../error'; |
5 | 5 | import type { OneOrMore } from '../mongo_types'; |
6 | 6 | import { ReadPreference } from '../read_preference'; |
7 | 7 | import type { Server } from '../sdam/server'; |
@@ -320,22 +320,23 @@ export class EnsureIndexOperation extends CreateIndexOperation { |
320 | 320 | override execute(server: Server, session: ClientSession | undefined, callback: Callback): void { |
321 | 321 | const indexName = this.indexes[0].name; |
322 | 322 | const cursor = this.db.collection(this.collectionName).listIndexes({ session }); |
323 | | - cursor.toArray((err, indexes) => { |
324 | | - /// ignore "NamespaceNotFound" errors |
325 | | - if (err && (err as MongoServerError).code !== MONGODB_ERROR_CODES.NamespaceNotFound) { |
326 | | - return callback(err); |
327 | | - } |
328 | | - |
329 | | - if (indexes) { |
| 323 | + cursor.toArray().then( |
| 324 | + indexes => { |
330 | 325 | indexes = Array.isArray(indexes) ? indexes : [indexes]; |
331 | 326 | if (indexes.some(index => index.name === indexName)) { |
332 | 327 | callback(undefined, indexName); |
333 | 328 | return; |
334 | 329 | } |
| 330 | + super.execute(server, session, callback); |
| 331 | + }, |
| 332 | + error => { |
| 333 | + if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) { |
| 334 | + // ignore "NamespaceNotFound" errors |
| 335 | + return super.execute(server, session, callback); |
| 336 | + } |
| 337 | + return callback(error); |
335 | 338 | } |
336 | | - |
337 | | - super.execute(server, session, callback); |
338 | | - }); |
| 339 | + ); |
339 | 340 | } |
340 | 341 | } |
341 | 342 |
|
|
0 commit comments