{"record":{"id":"9ac6e73c31d62065","repo":"plandex-ai/plandex","slug":"error-getting-branches-9ac6e7","errorCode":null,"errorMessage":"Error getting branches: ","messagePattern":"Error getting branches: ","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/plans_crud.go","lineNumber":638,"sourceCode":"\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\n\t\torConditions = append(orConditions, fmt.Sprintf(\"(plan_id = $%d AND name = $%d)\", currentArg, currentArg+1))\n\t\tqueryArgs = append(queryArgs, plan.Id, branchName)\n\n\t\tcurrentArg += 2\n\t}\n\n\tquery += \"(\" + strings.Join(orConditions, \" OR \") + \") AND archived_at IS NULL AND deleted_at IS NULL\"\n\n\tvar branches []db.Branch\n\terr = db.Conn.Select(&branches, query, queryArgs...)\n\n\tif err != nil {\n\t\tlog.Printf(\"Error getting branches: %v\\n\", err)\n\t\thttp.Error(w, \"Error getting branches: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tres := map[string]*shared.Branch{}\n\tfor _, branch := range branches {\n\t\tres[branch.PlanId] = branch.ToApi()\n\t}\n\n\tbytes, err := json.Marshal(res)\n\n\tif err != nil {\n\t\tlog.Printf(\"Error marshalling branches: %v\\n\", err)\n\t\thttp.Error(w, \"Error marshalling branches: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tlog.Println(\"Successfully processed GetCurrentBranchByPlanIdHandler request\")\n","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/plans_crud.go#L620-L656","documentation":"A 500 returned when the hand-built branches SQL query fails to execute via db.Conn.Select. The handler constructs a dynamic 'SELECT * FROM branches WHERE (...)' with OR conditions; any SQL syntax or driver error surfaces here with the raw error text.","triggerScenarios":"Calling the endpoint when CurrentBranchByPlanId in the request maps to plan ids owned by the user but the query is malformed (e.g. empty orConditions list producing 'WHERE () AND ...'), Postgres is unreachable, or the branches table schema is out of sync.","commonSituations":"Sending a CurrentBranchByPlanId map whose keys match none of the owned plans, yielding an invalid empty WHERE clause; database outage; branch name with unexpected characters causing binding issues; schema drift after upgrade.","solutions":["Check the logged '%v' detail for the exact SQL/driver error","Ensure the request body includes a CurrentBranchByPlanId entry for at least one plan in the project (empty maps can produce an empty OR-condition list)","Verify Postgres connectivity and that the branches table exists with expected columns","Upgrade/patch the server: the handler should 400 early when no orConditions were built"],"exampleFix":"// before\nquery += \"(\" + strings.Join(orConditions, \" OR \") + \") AND archived_at IS NULL AND deleted_at IS NULL\"\n// after\nif len(orConditions) == 0 {\n\thttp.Error(w, \"No branch names provided\", http.StatusBadRequest)\n\treturn\n}\nquery += \"(\" + strings.Join(orConditions, \" OR \") + \") AND archived_at IS NULL AND deleted_at IS NULL\"","handlingStrategy":"validation","validationCode":"// only send entries for plans that exist and are owned by you\nconst owned = new Set((await listPlans(projectId)).map(p => p.id));\nconst body = { currentBranchByPlanId: Object.fromEntries(\n  Object.entries(currentBranchByPlanId).filter(([id, name]) =>\n    owned.has(id) && typeof name === 'string' && name.length > 0)) };\nif (Object.keys(body.currentBranchByPlanId).length === 0) {\n  throw new Error('no valid plan/branch pairs to query');\n}","typeGuard":"function isValidBranchRequest(body) {\n  return body && typeof body.currentBranchByPlanId === 'object' &&\n    Object.values(body.currentBranchByPlanId).every(v => typeof v === 'string' && v.length > 0);\n}","tryCatchPattern":"try {\n  const res = await api.getCurrentBranchByPlanId(projectId, body);\n} catch (err) {\n  if (err.status === 500 && String(err.message).startsWith('Error getting branches:')) {\n    console.error('Branch query failed; check server log for SQL detail', err.message);\n    return null;\n  }\n  throw err;\n}","preventionTips":["Never send an empty currentBranchByPlanId map (can produce a malformed WHERE clause)","Keep branches-table migrations in sync with the server version","Sanitize branch names coming from user input","Confirm Postgres connectivity before bulk branch queries"],"tags":["database","sql","http-500","plandex-server"],"backgroundTag":"database-query-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}