{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/gridfs/download.ts#L464-L484","documentation":"Defensive internal error in handleEndOption reached when the function is invoked without a defined options.end. The public API always defaults end to 0, so reaching this typically indicates direct manipulation of stream private state or a driver regression.","triggerScenarios":"Internal invocation of handleEndOption with options.end == null, or manual mutation of stream.s.options.end to null/undefined.","commonSituations":"Driver bug; tampering with stream.s.options directly; a fork that altered the init flow.","solutions":["Use only the public openDownloadStream API","Never assign stream.s.options.end = null/undefined manually","If reproduced on the latest driver, file a bug"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  for await (const chunk of stream) { /* ... */ }\n} catch (err) {\n  if (err instanceof MongoInvalidArgumentError && /End option must be defined/i.test(err.message)) {\n    // unexpected internal path; report and recreate the stream\n  } else throw err;\n}","preventionTips":["Use only public GridFS APIs","Do not mutate stream.s.options.end to null/undefined"],"tags":["gridfs","stream","internal"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}