File tree Expand file tree Collapse file tree 4 files changed +45
-7
lines changed Expand file tree Collapse file tree 4 files changed +45
-7
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,8 @@ import {
31
31
MongoshUnimplementedError ,
32
32
} from '@mongosh/errors' ;
33
33
import type { ClientEncryption } from './field-level-encryption' ;
34
+ import type { MongoServerError } from 'mongodb' ;
35
+
34
36
chai . use ( sinonChai ) ;
35
37
36
38
describe ( 'Database' , function ( ) {
@@ -323,6 +325,21 @@ describe('Database', function () {
323
325
}
324
326
) ;
325
327
} ) ;
328
+
329
+ it ( 'rephrases the "NotPrimaryNoSecondaryOk" error' , async function ( ) {
330
+ const originalError : Partial < MongoServerError > = {
331
+ message : 'old message' ,
332
+ codeName : 'NotPrimaryNoSecondaryOk' ,
333
+ code : 13435 ,
334
+ } ;
335
+ serviceProvider . runCommandWithCheck . rejects ( originalError ) ;
336
+ const caughtError = await database
337
+ . runCommand ( { someCommand : 'someCollection' } )
338
+ . catch ( ( e ) => e ) ;
339
+ expect ( caughtError . message ) . to . contain (
340
+ 'e.g. db.runCommand({ command }, { readPreference: "secondaryPreferred" })'
341
+ ) ;
342
+ } ) ;
326
343
} ) ;
327
344
328
345
describe ( 'adminCommand' , function ( ) {
Original file line number Diff line number Diff line change @@ -365,11 +365,19 @@ export default class Database extends ShellApiWithMongoClass {
365
365
cmd = { [ cmd ] : 1 } ;
366
366
}
367
367
368
- const hiddenCommands = new RegExp ( HIDDEN_COMMANDS ) ;
369
- if ( ! Object . keys ( cmd ) . some ( ( k ) => hiddenCommands . test ( k ) ) ) {
370
- this . _emitDatabaseApiCall ( 'runCommand' , { cmd, options } ) ;
368
+ try {
369
+ const hiddenCommands = new RegExp ( HIDDEN_COMMANDS ) ;
370
+ if ( ! Object . keys ( cmd ) . some ( ( k ) => hiddenCommands . test ( k ) ) ) {
371
+ this . _emitDatabaseApiCall ( 'runCommand' , { cmd, options } ) ;
372
+ }
373
+ return await this . _runCommand ( cmd , options ) ;
374
+ } catch ( error : any ) {
375
+ if ( error . codeName === 'NotPrimaryNoSecondaryOk' ) {
376
+ const message = `not primary - consider passing the readPreference option e.g. db.runCommand({ command }, { readPreference: "secondaryPreferred" })` ;
377
+ ( error as Error ) . message = message ;
378
+ }
379
+ throw error ;
371
380
}
372
- return this . _runCommand ( cmd , options ) ;
373
381
}
374
382
375
383
/**
Original file line number Diff line number Diff line change @@ -61,6 +61,17 @@ describe('mongo-errors', function () {
61
61
expect ( r . code ) . to . equal ( 13435 ) ;
62
62
expect ( r . message ) . to . contain ( 'setReadPref' ) ;
63
63
} ) ;
64
+
65
+ it ( 'does not rephrase a NotPrimaryNoSecondaryOk error with db.runCommand example' , function ( ) {
66
+ const e = new MongoError (
67
+ 'not primary - consider passing the readPreference option e.g. db.runCommand({ command }, { readPreference: "secondaryPreferred" })'
68
+ ) ;
69
+ e . code = 13435 ;
70
+ const r = rephraseMongoError ( e ) ;
71
+ expect ( r ) . to . equal ( e ) ;
72
+ expect ( r . code ) . to . equal ( 13435 ) ;
73
+ expect ( r . message ) . not . to . contain ( 'setReadPref' ) ;
74
+ } ) ;
64
75
} ) ;
65
76
} ) ;
66
77
@@ -101,7 +112,7 @@ describe('mongo-errors', function () {
101
112
expect . fail ( 'expected error' ) ;
102
113
} catch ( e : any ) {
103
114
expect ( e ) . to . equal ( error ) ;
104
- expect ( e . message ) . to . contain ( 'not primary and secondaryOk=false ' ) ;
115
+ expect ( e . message ) . to . contain ( 'not primary' ) ;
105
116
expect ( e . message ) . to . contain ( 'db.getMongo().setReadPref()' ) ;
106
117
expect ( e . message ) . to . contain ( 'readPreference' ) ;
107
118
}
Original file line number Diff line number Diff line change @@ -9,8 +9,10 @@ const ERROR_REPHRASES: MongoErrorRephrase[] = [
9
9
{
10
10
// NotPrimaryNoSecondaryOk (also used for old terminology)
11
11
code : 13435 ,
12
- replacement :
13
- 'not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string' ,
12
+ replacement : ( message ) =>
13
+ message . includes ( 'db.runCommand' )
14
+ ? message
15
+ : 'not primary - consider using db.getMongo().setReadPref() or readPreference in the connection string' ,
14
16
} ,
15
17
] ;
16
18
You can’t perform that action at this time.
0 commit comments