{"record":{"id":"fb90d934d6b9509a","repo":"Mintplex-Labs/anything-llm","slug":"filename-is-required","errorCode":null,"errorMessage":"Filename is required!","messagePattern":"Filename is required!","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"collector/utils/files/index.js","lineNumber":126,"sourceCode":"}\n\n/**\n * Writes a document to the server documents folder.\n * @param {Object} params - The parameters for the function.\n * @param {Object} params.data - The data to write to the file. Must look like a document object.\n * @param {string} params.filename - The name of the file to write to.\n * @param {string|null} params.destinationOverride - A forced destination to write to - will be honored if provided.\n * @param {Object} params.options - The options for the function.\n * @param {boolean} params.options.parseOnly - If true, the file will be written to the direct uploads folder instead of the documents folder. Will be ignored if destinationOverride is provided.\n * @returns {Object} - The data with the location added.\n */\nfunction writeToServerDocuments({\n  data = {},\n  filename,\n  destinationOverride = null,\n  options = {},\n}) {\n  if (!filename) throw new Error(\"Filename is required!\");\n\n  let destination = null;\n  if (destinationOverride) destination = path.resolve(destinationOverride);\n  else if (options.parseOnly) destination = path.resolve(directUploadsFolder);\n  else destination = path.resolve(documentsFolder, \"custom-documents\");\n\n  if (!fs.existsSync(destination))\n    fs.mkdirSync(destination, { recursive: true });\n  const safeFilename = sanitizeFileName(filename);\n  const destinationFilePath = normalizePath(\n    path.resolve(destination, safeFilename) + \".json\"\n  );\n\n  fs.writeFileSync(destinationFilePath, JSON.stringify(data, null, 4), {\n    encoding: \"utf-8\",\n  });\n\n  return {","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/collector/utils/files/index.js#L108-L144","documentation":"Thrown by `writeToServerDocuments` when `filename` is falsy. This function writes parsed document JSON to the server's documents directory, so a missing filename means it cannot construct a destination file path — it fails fast rather than writing to an arbitrary or empty name. The check is a required-argument guard, not a runtime/environment condition.","triggerScenarios":"Calling `writeToServerDocuments({ data })` with no filename; passing `filename: null`/`''`/`undefined`; a parser that produces documents without setting a name field; destructuring a record whose `title`/`name` key is absent and feeding it as filename.","commonSituations":"A new connector/parser forgets to set the filename; a PDF/URL ingest where the source has no extractable title; refactoring renames the field but misses this call site.","solutions":["Ensure every caller computes a non-empty filename (use the source file name, a UUID, or a slugified title).","Add a fallback such as `filename = filename || document-${Date.now()}` before the call.","Validate the filename upstream with a type check and reject ingestion jobs missing it.","Unit-test parsers to assert they always emit a filename."],"exampleFix":"// before\nawait writeToServerDocuments({ data, filename: record.title });\n\n// after\nconst filename = record.title || record.name || `doc-${path.basename(sourcePath) || uuidv4()}`;\nif (!filename) throw new Error('Cannot determine filename for document');\nawait writeToServerDocuments({ data, filename });","handlingStrategy":"validation","validationCode":"function withFilename(data, candidate) {\n  const filename = candidate || data.title || data.name || `doc-${Date.now()}`;\n  if (!filename) throw new Error('Cannot determine filename for document');\n  return { data, filename };\n}","typeGuard":"function hasNonEmptyFilename(arg) {\n  return typeof arg?.filename === 'string' && arg.filename.trim().length > 0;\n}","tryCatchPattern":"try {\n  await writeToServerDocuments({ data, filename });\n} catch (e) {\n  if (/Filename is required/i.test(e.message)) {\n    filename = `doc-${Date.now()}`;\n    await writeToServerDocuments({ data, filename });\n  } else throw e;\n}","preventionTips":["Always compute a filename from the source (file name, slugified title, or UUID) before calling.","Add a unit test asserting parsers always emit a filename.","Reject ingestion jobs without a name upstream."],"tags":["files","validation","input-validation","filesystem"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}