{"record":{"id":"dfedfd39ddc5712d","repo":"Mintplex-Labs/anything-llm","slug":"internal-server-error-dfedfd","errorCode":null,"errorMessage":"Internal Server Error","messagePattern":"Internal Server Error","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"server/endpoints/api/document/index.js","lineNumber":170,"sourceCode":"        }\n\n        Collector.log(\n          `Document ${originalname} uploaded processed and successfully. It is now available in documents.`\n        );\n        await Telemetry.sendTelemetry(\"document_uploaded\");\n        await EventLogs.logEvent(\"api_document_uploaded\", {\n          documentName: originalname,\n        });\n\n        if (!!addToWorkspaces)\n          await Document.api.uploadToWorkspace(\n            addToWorkspaces,\n            documents?.[0].location\n          );\n        response.status(200).json({ success: true, error: null, documents });\n      } catch (e) {\n        console.error(e.message, e);\n        response.sendStatus(500).end();\n      }\n    }\n  );\n\n  app.post(\n    \"/v1/document/upload/:folderName\",\n    [validApiKey, handleAPIFileUpload, validateWorkspaceSlugQuery],\n    async (request, response) => {\n      /*\n      #swagger.tags = ['Documents']\n      #swagger.description = 'Upload a new file to a specific folder in AnythingLLM to be parsed and prepared for embedding. If the folder does not exist, it will be created.'\n      #swagger.parameters['folderName'] = {\n        in: 'path',\n        description: 'Target folder path (defaults to \\\"custom-documents\\\" if not provided)',\n        required: true,\n        type: 'string',\n        example: 'my-folder'\n      }","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/endpoints/api/document/index.js#L152-L188","documentation":"The generic 500 catch of POST /v1/document/upload. It fires when anything inside the try throws after the Collector checks: request.file missing (originalname undefined), Collector.processDocument throwing, Telemetry.sendTelemetry/EventLogs.logEvent throwing, or Document.api.uploadToWorkspace failing when addToWorkspaces points at a bad workspace. Note the handler already returns a structured 500 for an offline collector or a processDocument failure, so this bare 500 specifically means an UNCAUGHT exception — most often a missing request.file.","triggerScenarios":"POST /v1/document/upload without a multipart file (request.file undefined -> `const { originalname } = request.file` throws); Collector.processDocument rejecting unexpectedly; addToWorkspaces set to a non-existent workspace so uploadToWorkspace throws; Telemetry/EventLogs throwing on an unrelated failure.","commonSituations":"Calling the endpoint with JSON body instead of multipart/form-data; missing the file field name the multer middleware expects; Collector (document-processing sidecar) crashing mid-parse; addToWorkspaces pointing at a deleted workspace; network blip to the Collector.","solutions":["Send the request as multipart/form-data with a file under the field name the middleware expects — a missing request.file is the most common cause.","Confirm the Collector sidecar is reachable: GET /v1/system/health or the collector online check; an offline collector yields a structured 500, but a crashing one yields this bare 500.","Omit addToWorkspaces or verify the target workspace exists before auto-distributing the document.","Read the server log (console.error(e.message, e)) — this catch prints both the message and the full error."],"exampleFix":"// before — JSON body, no file -> request.file undefined -> 500\nawait fetch('/v1/document/upload', {\n  method: 'POST',\n  headers: { Authorization: 'Bearer ' + apiKey, 'Content-Type': 'application/json' },\n  body: JSON.stringify({})\n});\n\n// after — multipart form with a real file\nconst form = new FormData();\nform.append('file', fs.createReadStream('./doc.txt'));\nawait fetch('/v1/document/upload', {\n  method: 'POST',\n  headers: { Authorization: 'Bearer ' + apiKey },\n  body: form\n});","handlingStrategy":"validation","validationCode":"function isMultipartFileRequest(init) {\n  const ct = (init.headers?.['Content-Type'] || init.headers?.['content-type'] || '');\n  return ct.includes('multipart/form-data') && init.body instanceof FormData && Array.from(init.body.keys()).length > 0;\n}","typeGuard":"function hasFileField(form) { return form instanceof FormData && Array.from(form.keys()).includes('file'); }","tryCatchPattern":"try {\n  const r = await fetch('/v1/document/upload', { method: 'POST', headers, body: form });\n  if (r.status === 500) {\n    // structured 500 = collector offline / processDocument failure (has JSON body)\n    // bare 500 (empty body) = uncaught throw, most often missing request.file\n    const text = await r.text();\n    throw new Error(text ? ('collector error: ' + text) : 'uncaught 500 — likely missing multipart file; see server logs');\n  }\n  return await r.json();\n} catch (e) { throw e; }","preventionTips":["Always send multipart/form-data with a file field — a missing request.file is the top cause of this bare 500.","Confirm the Collector sidecar is online before bulk uploads.","Omit or pre-validate addToWorkspaces to avoid uploadToWorkspace failures.","Differentiate the structured 500 (collector offline, JSON body) from the bare 500 (uncaught throw)."],"tags":["document-api","file-upload","collector","validation","error-handling"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}