{"id":"496898a35feffd1b","repo":"mongodb/node-mongodb-native","slug":"unexpected-readpreference-mode-should-never-ha","errorCode":null,"errorMessage":"unexpected readPreference=${mode} (should never happen).  Please report a bug in the Node driver Jira project.","messagePattern":"unexpected readPreference=(.+?) \\(should never happen\\)\\.  Please report a bug in the Node driver Jira project\\.","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sdam/server_selection.ts","lineNumber":371,"sourceCode":"\n        // unlike readPreference=primary, here we do filter for deprioritized servers.\n        // if the primary is deprioritized, deprioritized secondaries take precedence.\n        const eligiblePrimary = primary.filter(isDeprioritizedFactory(deprioritized));\n        if (eligiblePrimary.length) return eligiblePrimary;\n\n        // we have no eligible primary nor secondaries that have not been deprioritized\n        return secondaries.length\n          ? latencyWindowReducer(topologyDescription, secondaries)\n          : primary;\n      }\n\n      // return all secondaries in the latency window.\n      return latencyWindowReducer(topologyDescription, secondaries);\n    }\n\n    default: {\n      const _exhaustiveCheck: never = mode;\n      throw new MongoRuntimeError(\n        `unexpected readPreference=${mode} (should never happen).  Please report a bug in the Node driver Jira project.`\n      );\n    }\n  }\n}\n\n/**\n * Returns a function which selects servers based on a provided read preference\n *\n * @param readPreference - The read preference to select with\n */\nexport function readPreferenceServerSelector(readPreference: ReadPreference): ServerSelector {\n  if (!readPreference.isValid()) {\n    throw new MongoInvalidArgumentError('Invalid read preference specified');\n  }\n\n  return function readPreferenceServers(\n    topologyDescription: TopologyDescription,","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sdam/server_selection.ts#L353-L389","documentation":"The `default` branch of the read-preference switch in `secondarySelector`, guarded by a TypeScript `never` exhaustive check on `mode` (server_selection.ts:369). It can only fire if a read preference mode outside the five known values reaches selection — i.e. the type system was bypassed. The message explicitly asks for a bug report.","triggerScenarios":"A custom/monkeypatched ReadPreference carrying an unknown mode string cast through `as ReadPreferenceMode`; an internal corruption of the mode field; using a future driver feature with an outdated type declaration.","commonSituations":"Casting an arbitrary string to the mode type; monkeypatching internals; an incompatible fork of the driver.","solutions":["Stop passing non-standard mode strings; use only the five documented modes.","Audit any code that casts or constructs ReadPreference with `as`.","Report a bug with the driver version and the offending mode value if it occurs with normal usage."],"exampleFix":"// before\nnew ReadPreference('fastest' as ReadPreferenceMode);\n// after\nnew ReadPreference('nearest');","handlingStrategy":"type-guard","validationCode":"const MODES = ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'];\nfunction readPref(mode) {\n  if (!MODES.includes(mode)) throw new TypeError('Unknown read preference mode: ' + mode);\n  return new ReadPreference(mode);\n}","typeGuard":"function isKnownMode(mode) {\n  return ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'].includes(mode);\n}","tryCatchPattern":null,"preventionTips":["Never cast arbitrary strings to ReadPreferenceMode.","Build modes only from the ReadPreference static constants.","Enable strict TypeScript checks to surface bad casts."],"tags":["internal","bug","read-preference"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}