{"record":{"id":"74ad0a0d9aee49d5","repo":"plandex-ai/plandex","slug":"error-creating-branch-v","errorCode":null,"errorMessage":"error creating branch: %v","messagePattern":"error creating branch: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/branches.go","lineNumber":142,"sourceCode":"\n\tctx, cancel := context.WithCancel(r.Context())\n\n\terr = db.ExecRepoOperation(db.ExecRepoOperationParams{\n\t\tOrgId:    auth.OrgId,\n\t\tUserId:   auth.User.Id,\n\t\tPlanId:   planId,\n\t\tBranch:   \"main\",\n\t\tReason:   \"create branch\",\n\t\tScope:    db.LockScopeWrite,\n\t\tCtx:      ctx,\n\t\tCancelFn: cancel,\n\t}, func(repo *db.GitRepo) error {\n\n\t\terr := db.WithTx(ctx, \"create branch\", func(tx *sqlx.Tx) error {\n\t\t\t_, err = db.CreateBranch(repo, plan, parentBranch, req.Name, tx)\n\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"error creating branch: %v\", err)\n\t\t\t}\n\n\t\t\treturn nil\n\t\t})\n\n\t\treturn err\n\t})\n\n\tif err != nil {\n\t\tlog.Printf(\"Error creating branch: %v\\n\", err)\n\t\thttp.Error(w, \"Error creating branch: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tlog.Println(\"Successfully created branch\")\n}\n\nfunc DeleteBranchHandler(w http.ResponseWriter, r *http.Request) {","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/branches.go#L124-L160","documentation":"Wraps a failure from db.CreateBranch inside the 'create branch' transaction. The branch could not be created for the given repo/plan/parent — e.g. duplicate name, missing parent, or DB constraint failure — causing the whole WithTx block to roll back.","triggerScenarios":"Branch creation API runs the WithTx callback; CreateBranch returns an error such as a unique-constraint violation on branch name, invalid parentBranch reference, or underlying SQL error.","commonSituations":"Client tries to create a branch whose name already exists; parent branch deleted or renamed concurrently; DB connectivity issue mid-transaction; name violates constraints (length/charset).","solutions":["Check the wrapped %v cause — most often a duplicate-name constraint violation","Use a unique branch name or verify the branch does not already exist","Confirm parentBranch still exists and is accessible in this repo","Retry if the error was transient (deadlock/connection) — WithTx rolls back cleanly"],"exampleFix":"// before\n_, err = db.CreateBranch(repo, plan, parentBranch, req.Name, tx)\nif err != nil {\n\treturn fmt.Errorf(\"error creating branch: %v\", err)\n}\n// after\n_, err = db.CreateBranch(repo, plan, parentBranch, req.Name, tx)\nif err != nil {\n\tif strings.Contains(err.Error(), \"duplicate\" ) {\n\t\treturn fmt.Errorf(\"branch %q already exists\", req.Name)\n\t}\n\treturn fmt.Errorf(\"error creating branch: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"// pre-check before the transaction\nexists, _ := db.BranchExists(repo, req.Name)\nif exists {\n\treturn fmt.Errorf(\"branch %q already exists\", req.Name)\n}","typeGuard":null,"tryCatchPattern":"err := CreateBranchHandler(w, r, req)\nif err != nil && strings.Contains(err.Error(), \"error creating branch\") {\n\tif strings.Contains(err.Error(), \"duplicate\") || strings.Contains(err.Error(), \"unique\") {\n\t\thttp.Error(w, \"branch name already in use\", http.StatusConflict)\n\t\treturn\n\t}\n\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n}","preventionTips":["Uniqueness-check branch names client-side before submit","Validate parent branch existence before invoking creation","Classify constraint violations as 409 vs transient errors as retryable 5xx","Rely on WithTx rollback so failed creates leave no partial state"],"tags":["database","branches","transaction"],"backgroundTag":"branch-creation-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}