|
| 1 | +import { MongoError } from 'mongodb'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { rephraseMongoError } from './mongo-errors'; |
| 4 | +import Mongo from './mongo'; |
| 5 | +import { StubbedInstance, stubInterface } from 'ts-sinon'; |
| 6 | +import { bson, ServiceProvider } from '@mongosh/service-provider-core'; |
| 7 | +import Database from './database'; |
| 8 | +import { EventEmitter } from 'events'; |
| 9 | +import ShellInternalState from './shell-internal-state'; |
| 10 | +import Collection from './collection'; |
| 11 | + |
| 12 | +class MongoshInternalError extends Error { |
| 13 | + constructor(message: string) { |
| 14 | + super(message); |
| 15 | + this.name = 'MongoshInternalError'; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('mongo-errors', () => { |
| 20 | + describe('rephraseMongoError', () => { |
| 21 | + context('for primitive "errors"', () => { |
| 22 | + [ |
| 23 | + true, |
| 24 | + 42, |
| 25 | + 'a message', |
| 26 | + { some: 'object' } |
| 27 | + ].forEach(e => { |
| 28 | + it(`skips ${JSON.stringify(e)}`, () => { |
| 29 | + expect(rephraseMongoError(e)).to.equal(e); |
| 30 | + }); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + context('for non-MongoError errors', () => { |
| 35 | + [ |
| 36 | + new Error('an error'), |
| 37 | + Object.assign(new MongoshInternalError('Dummy error'), { code: 13435 }) |
| 38 | + ].forEach(e => { |
| 39 | + it(`ignores ${e.constructor.name} ${JSON.stringify(e)}`, () => { |
| 40 | + const origMessage = e.message; |
| 41 | + const r = rephraseMongoError(e); |
| 42 | + expect(r).to.equal(r); |
| 43 | + expect(r.message).to.equal(origMessage); |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + context('for MongoError errors', () => { |
| 49 | + it('ignores an irrelevant error', () => { |
| 50 | + const e = new MongoError('ignored'); |
| 51 | + const r = rephraseMongoError(e); |
| 52 | + expect(r).to.equal(e); |
| 53 | + expect(r.message).to.equal('ignored'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('rephrases a NotPrimaryNoSecondaryOk error', () => { |
| 57 | + const e = new MongoError('not master and slaveOk=false'); |
| 58 | + e.code = 13435; |
| 59 | + const r = rephraseMongoError(e); |
| 60 | + expect(r).to.equal(e); |
| 61 | + expect(r.code).to.equal(13435); |
| 62 | + expect(r.message).to.contain('setReadPref'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('intercepts shell API calls', () => { |
| 68 | + let mongo: Mongo; |
| 69 | + let serviceProvider: StubbedInstance<ServiceProvider>; |
| 70 | + let database: Database; |
| 71 | + let bus: StubbedInstance<EventEmitter>; |
| 72 | + let internalState: ShellInternalState; |
| 73 | + let collection: Collection; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + bus = stubInterface<EventEmitter>(); |
| 77 | + serviceProvider = stubInterface<ServiceProvider>(); |
| 78 | + serviceProvider.runCommand.resolves({ ok: 1 }); |
| 79 | + serviceProvider.runCommandWithCheck.resolves({ ok: 1 }); |
| 80 | + serviceProvider.initialDb = 'test'; |
| 81 | + serviceProvider.bsonLibrary = bson; |
| 82 | + internalState = new ShellInternalState(serviceProvider, bus); |
| 83 | + mongo = new Mongo(internalState, undefined, undefined, undefined, serviceProvider); |
| 84 | + database = new Database(mongo, 'db1'); |
| 85 | + collection = new Collection(mongo, database, 'coll1'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('on collection.find error', async() => { |
| 89 | + const error = new MongoError('not master and slaveOk=false'); |
| 90 | + error.code = 13435; |
| 91 | + serviceProvider.insertOne.rejects(error); |
| 92 | + |
| 93 | + try { |
| 94 | + await collection.insertOne({ fails: true }); |
| 95 | + expect.fail('expected error'); |
| 96 | + } catch (e) { |
| 97 | + expect(e).to.equal(error); |
| 98 | + expect(e.message).to.contain('not primary and secondaryOk=false'); |
| 99 | + expect(e.message).to.contain('db.getMongo().setReadPref()'); |
| 100 | + expect(e.message).to.contain('readPref'); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments