{"record":{"id":"dec31410276c41d7","repo":"Mintplex-Labs/anything-llm","slug":"folder-by-that-name-already-exists-dec314","errorCode":null,"errorMessage":"Folder by that name already exists","messagePattern":"Folder by that name already exists","errorType":"http","errorClass":null,"httpStatus":500,"severity":"warning","filePath":"server/endpoints/document.js","lineNumber":25,"sourceCode":"} = require(\"../utils/middleware/multiUserProtected\");\nconst { validatedRequest } = require(\"../utils/middleware/validatedRequest\");\nconst fs = require(\"fs\");\nconst path = require(\"path\");\n\nfunction documentEndpoints(app) {\n  if (!app) return;\n  app.post(\n    \"/document/create-folder\",\n    [validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],\n    async (request, response) => {\n      try {\n        const { name } = reqBody(request);\n        const storagePath = path.join(documentsPath, normalizePath(name));\n        if (!isWithin(path.resolve(documentsPath), path.resolve(storagePath)))\n          throw new Error(\"Invalid folder name.\");\n\n        if (fs.existsSync(storagePath)) {\n          response.status(500).json({\n            success: false,\n            message: \"Folder by that name already exists\",\n          });\n          return;\n        }\n\n        fs.mkdirSync(storagePath, { recursive: true });\n        response.status(200).json({ success: true, message: null });\n      } catch (e) {\n        console.error(e);\n        response.status(500).json({\n          success: false,\n          message: `Failed to create folder: ${e.message} `,\n        });\n      }\n    }\n  );\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/endpoints/document.js#L7-L43","documentation":"Returned by POST /document/create-folder (admin/manager) when fs.existsSync(storagePath) is true — a folder OR file with that normalized name already exists under documentsPath. It is an explicit duplicate check, not an exception; the 500 status is misleading (409 Conflict would be correct).","triggerScenarios":"POST /document/create-folder with {name} that collides with any existing entry in the documents storage directory. existsSync matches files too, so naming a folder like an existing file also triggers it.","commonSituations":"Double-submitting the 'Create folder' button in the UI; retrying a request that timed out but actually succeeded; scripted folder provisioning re-running without idempotency checks.","solutions":["Use a different folder name, or open the existing folder instead of creating a new one","List the documents directory to confirm what collides — it may be a file with the same name","Client-side: treat this exact message as a duplicate conflict, not a server fault","Server improvement: return 409 with this body instead of 500 so callers can branch on status"],"exampleFix":"// before (server/endpoints/document.js)\nresponse.status(500).json({ success: false, message: 'Folder by that name already exists' });\n// after\nresponse.status(409).json({ success: false, message: 'Folder by that name already exists' });","handlingStrategy":"validation","validationCode":"// Check current folder names before creating\nconst existing = new Set(folders.map((f) => f.name.toLowerCase()));\nif (existing.has(name.trim().toLowerCase())) { selectFolder(name); return; } // idempotent skip\nawait fetch('/document/create-folder', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }) });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Disable the Create button during submission to prevent double-posts","Match names case-insensitively against the fetched folder list first","Treat this message as a duplicate conflict (semantically 409), not a server error"],"tags":["documents","filesystem","duplicate","http-500"],"backgroundTag":"duplicate-resource-conflict","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}