{"record":{"id":"3f8b27edbd6a63fe","repo":"mongodb/node-mongodb-native","slug":"end-option-must-be-defined","errorCode":null,"errorMessage":"End option must be defined","messagePattern":"End option must be defined","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/gridfs/download.ts","lineNumber":482,"sourceCode":"  if (options && options.end != null) {\n    if (options.end > doc.length) {\n      throw new MongoInvalidArgumentError(\n        `Stream end (${options.end}) must not be more than the length of the file (${doc.length})`\n      );\n    }\n    if (options.start == null || options.start < 0) {\n      throw new MongoInvalidArgumentError(`Stream end (${options.end}) must not be negative`);\n    }\n\n    const start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0;\n\n    cursor.limit(Math.ceil(options.end / doc.chunkSize) - start);\n\n    stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize);\n\n    return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end;\n  }\n  throw new MongoInvalidArgumentError('End option must be defined');\n}\n","sourceCodeStart":464,"sourceCodeEnd":484,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/gridfs/download.ts#L464-L484","documentation":"handleEndOption (src/gridfs/download.ts:482) is a defensive internal guard that throws 'End option must be defined' when the helper is reached without an end value. Like error 269, this is an internal invariant; normal user calls provide end explicitly or omit it (in which case the helper is not invoked). Encountering it suggests internal misuse or a mutated options object.","triggerScenarios":"Internal/custom subclass invoking handleEndOption without an end value. Mutation of the stream options object removing end between construction and initialization.","commonSituations":"Rare for end users; appears only with subclassing or direct manipulation of GridFSBucketReadStream internals.","solutions":["Do not subclass or mutate stream internals; provide end via openDownloadStream(id, { end }).","If reached through a supported path, ensure the options object always includes end when range reading is intended."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Internal guard. Do not subclass GridFSBucketReadStream.\n// Provide end via openDownloadStream options.\nfunction openSafeEnd(bucket, id, range) {\n  if (!range || range.end == null) return bucket.openDownloadStream(id);\n  return bucket.openDownloadStream(id, range);\n}","typeGuard":"function hasEnd(range: unknown): range is { end: number } {\n  return range != null && typeof (range as any).end === 'number';\n}","tryCatchPattern":null,"preventionTips":["Do not subclass or mutate stream internals.","Provide end via the openDownloadStream options object.","Report a bug if this surfaces through a normal public API path."],"tags":["gridfs","stream","internal","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}