{"record":{"id":"d784bd48b0d739bf","repo":"mongodb/node-mongodb-native","slug":"stream-start-options-start-must-not-be-greate","errorCode":null,"errorMessage":"Stream start (${options.start}) must not be greater than stream end (${options.end})","messagePattern":"Stream start \\((.+?)\\) must not be greater than stream end \\((.+?)\\)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/gridfs/download.ts","lineNumber":445,"sourceCode":"  });\n}\n\nfunction handleStartOption(\n  stream: GridFSBucketReadStream,\n  doc: Document,\n  options: GridFSBucketReadStreamOptions\n): number {\n  if (options && options.start != null) {\n    if (options.start > doc.length) {\n      throw new MongoInvalidArgumentError(\n        `Stream start (${options.start}) must not be more than the length of the file (${doc.length})`\n      );\n    }\n    if (options.start < 0) {\n      throw new MongoInvalidArgumentError(`Stream start (${options.start}) must not be negative`);\n    }\n    if (options.end != null && options.end < options.start) {\n      throw new MongoInvalidArgumentError(\n        `Stream start (${options.start}) must not be greater than stream end (${options.end})`\n      );\n    }\n\n    stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize;\n    stream.s.expected = Math.floor(options.start / doc.chunkSize);\n\n    return options.start - stream.s.bytesRead;\n  }\n  throw new MongoInvalidArgumentError('Start option must be defined');\n}\n\nfunction handleEndOption(\n  stream: GridFSBucketReadStream,\n  doc: Document,\n  cursor: FindCursor<GridFSChunk>,\n  options: GridFSBucketReadStreamOptions\n) {","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/gridfs/download.ts#L427-L463","documentation":"handleStartOption (src/gridfs/download.ts:444) checks that start does not exceed end. If both are provided and end < start, it throws MongoInvalidArgumentError because the byte range would be inverted/empty.","triggerScenarios":"Calling openDownloadStream(id, { start: 100, end: 50 }) or stream.start(100).end(50). Passing swapped start/end arguments.","commonSituations":"Variable assignment where start and end are filled from a request in the wrong order. Resume logic that computes end before start and gets the comparison backwards.","solutions":["Ensure start <= end; swap or clamp values before opening the stream.","Validate range inputs (start <= end) at the application boundary."],"exampleFix":"// before\nconst s = bucket.openDownloadStream(id, { start: 100, end: 50 });\n// after\nconst s = bucket.openDownloadStream(id, { start: Math.min(a, b), end: Math.max(a, b) });","handlingStrategy":"validation","validationCode":"function normalizeRange(start, end) {\n  if (start != null && end != null && end < start) [start, end] = [end, start];\n  return { start, end };\n}","typeGuard":"function isOrderedRange(start: number, end: number): boolean {\n  return start <= end;\n}","tryCatchPattern":"try {\n  bucket.openDownloadStream(id, { start, end });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /greater than stream end/.test(e.message)) {\n    [start, end] = [end, start];\n  }\n}","preventionTips":["Always order range bounds (min/max) before passing to the stream.","Validate start <= end at the request boundary.","Use named parameters/object destructuring to avoid swapping start and end."],"tags":["gridfs","stream","validation","range"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}