plandex-ai/plandex · error

Error getting branches:

Error message

Error getting branches: 

What it means

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.

Source

Thrown at app/server/handlers/plans_crud.go:638

		if !ok {
			continue
		}

		orConditions = append(orConditions, fmt.Sprintf("(plan_id = $%d AND name = $%d)", currentArg, currentArg+1))
		queryArgs = append(queryArgs, plan.Id, branchName)

		currentArg += 2
	}

	query += "(" + strings.Join(orConditions, " OR ") + ") AND archived_at IS NULL AND deleted_at IS NULL"

	var branches []db.Branch
	err = db.Conn.Select(&branches, query, queryArgs...)

	if err != nil {
		log.Printf("Error getting branches: %v\n", err)
		http.Error(w, "Error getting branches: "+err.Error(), http.StatusInternalServerError)
		return
	}

	res := map[string]*shared.Branch{}
	for _, branch := range branches {
		res[branch.PlanId] = branch.ToApi()
	}

	bytes, err := json.Marshal(res)

	if err != nil {
		log.Printf("Error marshalling branches: %v\n", err)
		http.Error(w, "Error marshalling branches: "+err.Error(), http.StatusInternalServerError)
		return
	}

	log.Println("Successfully processed GetCurrentBranchByPlanIdHandler request")

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check the logged '%v' detail for the exact SQL/driver error
  2. 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)
  3. Verify Postgres connectivity and that the branches table exists with expected columns
  4. Upgrade/patch the server: the handler should 400 early when no orConditions were built

Example fix

// before
query += "(" + strings.Join(orConditions, " OR ") + ") AND archived_at IS NULL AND deleted_at IS NULL"
// after
if len(orConditions) == 0 {
	http.Error(w, "No branch names provided", http.StatusBadRequest)
	return
}
query += "(" + strings.Join(orConditions, " OR ") + ") AND archived_at IS NULL AND deleted_at IS NULL"
Defensive patterns

Strategy: validation

Validate before calling

// only send entries for plans that exist and are owned by you
const owned = new Set((await listPlans(projectId)).map(p => p.id));
const body = { currentBranchByPlanId: Object.fromEntries(
  Object.entries(currentBranchByPlanId).filter(([id, name]) =>
    owned.has(id) && typeof name === 'string' && name.length > 0)) };
if (Object.keys(body.currentBranchByPlanId).length === 0) {
  throw new Error('no valid plan/branch pairs to query');
}

Type guard

function isValidBranchRequest(body) {
  return body && typeof body.currentBranchByPlanId === 'object' &&
    Object.values(body.currentBranchByPlanId).every(v => typeof v === 'string' && v.length > 0);
}

Try / catch

try {
  const res = await api.getCurrentBranchByPlanId(projectId, body);
} catch (err) {
  if (err.status === 500 && String(err.message).startsWith('Error getting branches:')) {
    console.error('Branch query failed; check server log for SQL detail', err.message);
    return null;
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/9ac6e73c31d62065. Report an issue: GitHub.