{"record":{"id":"6b3e4a9e37664e52","repo":"mongodb/node-mongodb-native","slug":"cannot-parse-namespace-from-namespace","errorCode":null,"errorMessage":"Cannot parse namespace from \"${namespace}\"","messagePattern":"Cannot parse namespace from \"(.+?)\"","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/utils.ts","lineNumber":278,"sourceCode":"   * @param collection - collection name\n   */\n  constructor(db: string, collection?: string) {\n    this.db = db;\n    this.collection = collection === '' ? undefined : collection;\n  }\n\n  toString(): string {\n    return this.collection ? `${this.db}.${this.collection}` : this.db;\n  }\n\n  withCollection(collection: string): MongoDBCollectionNamespace {\n    return new MongoDBCollectionNamespace(this.db, collection);\n  }\n\n  static fromString(namespace?: string): MongoDBNamespace {\n    if (typeof namespace !== 'string' || namespace === '') {\n      // TODO(NODE-3483): Replace with MongoNamespaceError\n      throw new MongoRuntimeError(`Cannot parse namespace from \"${namespace}\"`);\n    }\n\n    const [db, ...collectionParts] = namespace.split('.');\n    const collection = collectionParts.join('.');\n    return new MongoDBNamespace(db, collection === '' ? undefined : collection);\n  }\n}\n\n/**\n * @public\n *\n * A class representing a collection's namespace.  This class enforces (through Typescript) that\n * the `collection` portion of the namespace is defined and should only be\n * used in scenarios where this can be guaranteed.\n */\nexport class MongoDBCollectionNamespace extends MongoDBNamespace {\n  override collection: string;\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/utils.ts#L260-L296","documentation":"Thrown by MongoDBNamespace.fromString() when the namespace argument is not a string or is an empty string. The parser splits on '.' to extract db and collection; an empty or non-string input has no valid db component. The TODO comment notes this should eventually be a MongoNamespaceError but currently raises MongoRuntimeError.","triggerScenarios":"Internal calls that build a namespace from an empty or undefined string, e.g. when a collection name is computed dynamically and resolves to ''. Rarely user-facing unless the namespace is passed through a public API that accepts arbitrary strings.","commonSituations":"Dynamic code that derives db or collection names from variables that may be undefined/empty. Using client.db('') or db.collection('') in some internal paths. Misconfigured environment variables for database/collection names.","solutions":["Ensure any namespace string passed contains at least a non-empty database name.","Validate db and collection names are non-empty strings before constructing namespace-dependent operations.","Check environment variables or config that feed into db/collection names."],"exampleFix":"// before\nconst dbName = process.env.DB_NAME; // undefined\nclient.db(dbName).collection('items');\n\n// after\nconst dbName = process.env.DB_NAME || 'default_db';\nclient.db(dbName).collection('items');","handlingStrategy":"validation","validationCode":"function requireNamespace(ns: string | undefined): string {\n  if (typeof ns !== 'string' || ns.trim() === '') throw new Error('Namespace must be a non-empty string');\n  return ns;\n}","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === 'string' && v.length > 0;\n}","tryCatchPattern":"try {\n  const ns = MongoDBNamespace.fromString(input);\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /Cannot parse namespace/.test(e.message)) {\n    // fall back to a safe default namespace\n  } else throw e;\n}","preventionTips":["Validate db and collection names are non-empty strings before use.","Provide fallback defaults for env-driven db/collection names.","Unit-test namespace construction with empty/undefined inputs."],"tags":["namespace","validation","configuration"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}