Skip to content

Commit

Permalink
fix(shell-api): mongosh should throw an error when trying to drop a n…
Browse files Browse the repository at this point in the history
…on-primary index MONGOSH-1608 (#2221)
  • Loading branch information
gagik authored Oct 18, 2024
1 parent d83f369 commit 15faeba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
21 changes: 11 additions & 10 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,26 +1165,27 @@ describe('Collection', function () {
context(
'when serviceProvider.dropIndexes rejects IndexNotFound',
function () {
let expectedError: Error;
beforeEach(function () {
const error = new Error('index not found with name [index_1]');
Object.assign(error, {
expectedError = new Error('index not found with name [index_1]');
Object.assign(expectedError, {
ok: 0,
errmsg: 'index not found with name [index_1]',
code: 27,
codeName: 'IndexNotFound',
name: 'MongoError',
name: 'MongoServerError',
});

serviceProvider.runCommandWithCheck.rejects(error);
serviceProvider.runCommandWithCheck.rejects(expectedError);
});

it('returns the error as object', async function () {
expect(await collection.dropIndexes('index_1')).to.deep.equal({
ok: 0,
errmsg: 'index not found with name [index_1]',
code: 27,
codeName: 'IndexNotFound',
});
let caughtError: Error | undefined;
await collection
.dropIndexes('index_1')
.catch((err) => (caughtError = err));

expect(caughtError).to.deep.equal(expectedError);
});
}
);
Expand Down
9 changes: 0 additions & 9 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,15 +1335,6 @@ export default class Collection extends ShellApiWithMongoClass {
return all.sort((a, b) => b.nIndexesWas - a.nIndexesWas)[0];
}

if (error?.codeName === 'IndexNotFound') {
return {
ok: error.ok,
errmsg: error.errmsg,
code: error.code,
codeName: error.codeName,
};
}

throw error;
}
}
Expand Down

0 comments on commit 15faeba

Please sign in to comment.