{"id":"b716dce0b2f449e8","repo":"mongodb/node-mongodb-native","slug":"maxstalenessseconds-must-be-a-positive-integer","errorCode":null,"errorMessage":"maxStalenessSeconds must be a positive integer","messagePattern":"maxStalenessSeconds must be a positive integer","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/read_preference.ts","lineNumber":103,"sourceCode":"    if (!ReadPreference.isValid(mode)) {\n      throw new MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`);\n    }\n    if (options == null && typeof tags === 'object' && !Array.isArray(tags)) {\n      options = tags;\n      tags = undefined;\n    } else if (tags && !Array.isArray(tags)) {\n      throw new MongoInvalidArgumentError('ReadPreference tags must be an array');\n    }\n\n    this.mode = mode;\n    this.tags = tags;\n    this.hedge = options?.hedge;\n    this.maxStalenessSeconds = undefined;\n\n    options = options ?? {};\n    if (options.maxStalenessSeconds != null) {\n      if (options.maxStalenessSeconds <= 0) {\n        throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer');\n      }\n\n      this.maxStalenessSeconds = options.maxStalenessSeconds;\n    }\n\n    if (this.mode === ReadPreference.PRIMARY) {\n      if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {\n        throw new MongoInvalidArgumentError('Primary read preference cannot be combined with tags');\n      }\n\n      if (this.maxStalenessSeconds) {\n        throw new MongoInvalidArgumentError(\n          'Primary read preference cannot be combined with maxStalenessSeconds'\n        );\n      }\n\n      if (this.hedge) {\n        throw new MongoInvalidArgumentError(","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/read_preference.ts#L85-L121","documentation":"Thrown by the ReadPreference constructor when options.maxStalenessSeconds is provided and is <= 0. Max staleness governs how stale a secondary can be for reads; it must be a positive integer (and per the server spec, effectively >= 90 seconds - the server enforces the 90s floor separately). The driver rejects non-positive values client-side. MongoInvalidArgumentError.","triggerScenarios":"Passing maxStalenessSeconds: 0 or a negative number; passing a value computed from arithmetic that underflows to 0/negative; passing a non-numeric that survives the null check but is <= 0 in comparison coercion.","commonSituations":"Config defaults of 0 meaning 'disabled' but interpreted literally; off-by-one or rounding producing 0; copying a value meant as milliseconds into a seconds field.","solutions":["Set maxStalenessSeconds to a positive integer (>= 90 to satisfy the server-side minimum).","Omit maxStalenessSeconds entirely if you don't want a staleness constraint.","Validate that computed values are positive integers before passing them in.","Double-check units - the field is seconds, not milliseconds."],"exampleFix":"// before\nnew ReadPreference('secondary', undefined, { maxStalenessSeconds: 0 });\n// after\nnew ReadPreference('secondary', undefined, { maxStalenessSeconds: 90 });","handlingStrategy":"validation","validationCode":"if (maxStalenessSeconds != null && !(Number.isInteger(maxStalenessSeconds) && maxStalenessSeconds > 0)) {\n  throw new Error('maxStalenessSeconds must be a positive integer (>=90)');\n}\nnew ReadPreference(mode, tags, { maxStalenessSeconds });","typeGuard":"function isPositiveInt(v: unknown): v is number {\n  return typeof v === 'number' && Number.isInteger(v) && v > 0;\n}","tryCatchPattern":null,"preventionTips":["Remember the field is seconds (not ms) and the server requires >= 90.","Omit maxStalenessSeconds to disable the staleness constraint.","Validate computed staleness values before passing them in."],"tags":["read-preference","max-staleness","validation","argument-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}