{"record":{"id":"a874273c6cb82728","repo":"vxcontrol/pentagi","slug":"failed-to-list-source-directory-w","errorCode":null,"errorMessage":"failed to list source directory: %w","messagePattern":"failed to list source directory: %w","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"backend/pkg/server/services/resources.go","lineNumber":1131,"sourceCode":"\t}\n\tif sourceRoot != nil {\n\t\tif err := tx.Delete(sourceRoot).Error; err != nil {\n\t\t\treturn result, fmt.Errorf(\"failed to delete source directory %q: %w\", sourceRoot.Path, err)\n\t\t}\n\t\tresult.DeletedAfter = append(result.DeletedAfter, convertResource(*sourceRoot))\n\t}\n\n\treturn result, nil\n}\n\nfunc listResourceTree(tx *gorm.DB, uid uint64, rootPath string) ([]models.UserResource, error) {\n\tescapedRoot := resources.EscapeLike(rootPath)\n\tvar entries []models.UserResource\n\tif err := tx.Where(\n\t\t\"user_id = ? AND (path = ? OR path LIKE ?)\",\n\t\tuid, rootPath, escapedRoot+\"/%\",\n\t).Order(\"path ASC\").Find(&entries).Error; err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to list source directory: %w\", err)\n\t}\n\treturn entries, nil\n}\n\nfunc findResourceByPath(tx *gorm.DB, uid uint64, resourcePath string) (models.UserResource, bool, error) {\n\tvar rec models.UserResource\n\terr := tx.Where(\"user_id = ? AND path = ?\", uid, resourcePath).First(&rec).Error\n\tif err == nil {\n\t\treturn rec, true, nil\n\t}\n\tif gorm.IsRecordNotFoundError(err) {\n\t\treturn models.UserResource{}, false, nil\n\t}\n\treturn models.UserResource{}, false, err\n}\n\nfunc findResourcesByPaths(tx *gorm.DB, uid uint64, paths []string) ([]models.UserResource, error) {\n\tif len(paths) == 0 {","sourceCodeStart":1113,"sourceCodeEnd":1149,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/server/services/resources.go#L1113-L1149","documentation":"listResourceTree loads the full contents of a directory (the directory row plus all descendants via path LIKE 'root/%') before a directory move or copy. This error wraps the GORM query failure that lists those entries, so the caller knows the tree could not be read and the operation aborts before any mutation.","triggerScenarios":"moveDirResource or copyDirResource calls listResourceTree and the SELECT on user_resources fails — DB connection loss, timeout on very large directories, table lock, or permission problem for the DB user.","commonSituations":"Extremely large directories making the LIKE scan slow enough to hit statement timeouts; database failover/restarts; read-replica lag or misconfigured credentials.","solutions":["Read the wrapped DB error to distinguish timeout vs connection vs permission","Increase statement_timeout or paginate the tree listing for very large directories","Verify DB connectivity/credentials and retry with backoff on transient errors"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if _, err := db.Raw(\"SELECT count(*) FROM user_resources WHERE path = ?\", rootPath).Rows(); err != nil {\n    // connectivity/permission problem: fail fast before the move\n}","typeGuard":null,"tryCatchPattern":"err := svc.MoveResource(ctx, uid, srcDir, dstDir, false)\nif err != nil && strings.Contains(err.Error(), \"failed to list source directory\") {\n    // backoff and retry; check DB health\n}","preventionTips":["Tune statement_timeout for large directory scans","Index or maintain path columns so LIKE 'root/%' scans stay fast","Monitor DB availability; add retry with backoff around moves"],"tags":["database","gorm","query","directory-listing"],"backgroundTag":"database-query-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}