{"record":{"id":"5db5453946f916af","repo":"OtterMind/Chat2DB","slug":"file-or-directory-already-exists","errorCode":null,"errorMessage":"File or directory already exists","messagePattern":"File or directory already exists","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"chat2db-community-server/chat2db-community-jcef/src/main/java/ai/chat2db/community/jcef/handler/biz/SqlDirectoryTreeStore.java","lineNumber":128,"sourceCode":"        Path root = getRoot(rootToken);\n        Path parent = resolveInRoot(root, parentRelativePath);\n        if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) {\n            throw new IllegalArgumentException(\"Selected path is not a directory\");\n        }\n\n        boolean directory = \"directory\".equals(type);\n        boolean file = \"file\".equals(type);\n        if (!directory && !file) {\n            throw new IllegalArgumentException(\"Unsupported SQL directory child type\");\n        }\n\n        String name = file ? normalizeFileName(rawName, \"sql\") : normalizeDirectoryName(rawName);\n        Path target = parent.resolve(name).normalize();\n        if (!target.startsWith(root)) {\n            throw new IllegalArgumentException(\"Path is outside of the selected SQL directory\");\n        }\n        if (Files.exists(target, LinkOption.NOFOLLOW_LINKS)) {\n            throw new IllegalArgumentException(\"File or directory already exists\");\n        }\n\n        if (directory) {\n            Files.createDirectory(target);\n        } else {\n            Files.createFile(target);\n        }\n\n        Path realTarget = target.toRealPath(LinkOption.NOFOLLOW_LINKS);\n        if (!realTarget.startsWith(root)) {\n            throw new IllegalArgumentException(\"Path is outside of the selected SQL directory\");\n        }\n\n        Map<String, Object> result = new HashMap<>();\n        result.put(\"createdNode\", toNode(rootToken, root, realTarget));\n        result.put(\"children\", listChildren(rootToken, parentRelativePath));\n        return result;\n    }","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-jcef/src/main/java/ai/chat2db/community/jcef/handler/biz/SqlDirectoryTreeStore.java#L110-L146","documentation":"Thrown by SqlDirectoryTreeStore.createChild() when the target file or directory already exists at the resolved path. After validating the parent and containment, the method checks Files.exists with NOFOLLOW_LINKS. If a file or directory with the same name already exists in the parent, creation is rejected to avoid overwriting.","triggerScenarios":"Invoked via create-sql-directory-child when a file or directory with the normalized name already exists in the parent directory. For files, normalizeFileName appends the fallback extension (e.g., '.sql'), so creating 'query' twice produces 'query.sql' both times and hits this guard.","commonSituations":"User creates a file with the same name as an existing one; the tree view is stale and does not show the already-existing entry; rapid double-submit of the same create action.","solutions":["Refresh the parent's children list before allowing the create action to show existing names","On the frontend, check for name collisions against the current children before submitting","Handle the error response by showing 'A file with this name already exists' and suggesting an alternative name"],"exampleFix":"// before: no duplicate check on frontend\nfunction onCreate(name, type) {\n  createChild(rootToken, parentPath, name, type);\n}\n\n// after: check existing children first\nfunction onCreate(name, type) {\n  const exists = children.some(c => c.name.toLowerCase() === name.toLowerCase());\n  if (exists) {\n    showError('A file or directory with this name already exists');\n    return;\n  }\n  createChild(rootToken, parentPath, name, type);\n}","handlingStrategy":"validation","validationCode":"// Before calling createChild, check for existing names\nPath target = parent.resolve(normalizedName);\nif (Files.exists(target, LinkOption.NOFOLLOW_LINKS)) {\n    throw new IllegalArgumentException(\"File or directory already exists: \" + normalizedName);\n}\nSqlDirectoryTreeStore.createChild(rootToken, parentRelativePath, name, type);","typeGuard":null,"tryCatchPattern":"try {\n    Map<String, Object> result = SqlDirectoryTreeStore.createChild(rootToken, parentRelativePath, name, type);\n    ResponseBuilder.buildSuccessJcef(Map.of(\"data\", result), callback);\n} catch (IllegalArgumentException e) {\n    callback.failure(409, e.getMessage()); // 409 Conflict is semantically correct\n}","preventionTips":["Refresh the parent's children list before showing the create dialog","On the frontend, check name collisions against the visible children","Return HTTP 409 Conflict for duplicate-name errors to distinguish from validation failures"],"tags":["sql-directory","duplicate","user-input","filesystem"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}