{"record":{"id":"b09ab92ee7981f91","repo":"plandex-ai/plandex","slug":"error-creating-user-v","errorCode":null,"errorMessage":"error creating user: %v","messagePattern":"error creating user: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/user_helpers.go","lineNumber":151,"sourceCode":"\temailSplit := strings.Split(email, \"@\")\n\tif len(emailSplit) != 2 {\n\t\treturn nil, fmt.Errorf(\"invalid email: %v\", email)\n\t}\n\tdomain := emailSplit[1]\n\n\tuser := User{\n\t\tName:   name,\n\t\tEmail:  email,\n\t\tDomain: domain,\n\t}\n\n\terr := tx.QueryRow(\"INSERT INTO users (name, email, domain) VALUES ($1, $2, $3) RETURNING id\", user.Name, user.Email, user.Domain).Scan(&user.Id)\n\n\tif err != nil {\n\t\tif IsNonUniqueErr(err) {\n\t\t\treturn nil, fmt.Errorf(\"user already exists for email: %v\", email)\n\t\t}\n\t\treturn nil, fmt.Errorf(\"error creating user: %v\", err)\n\t}\n\n\treturn &user, nil\n}\n\nfunc NumUsersWithRole(orgId, roleId string) (int, error) {\n\tvar count int\n\terr := Conn.Get(&count, \"SELECT COUNT(*) FROM orgs_users WHERE org_id = $1 AND org_role_id = $2\", orgId, roleId)\n\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"error counting users with role: %v\", err)\n\t}\n\n\treturn count, nil\n}\n","sourceCodeStart":133,"sourceCodeEnd":167,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/user_helpers.go#L133-L167","documentation":"Generic insertion-failure branch in CreateUser: any INSERT error that is not a unique-constraint violation (per IsNonUniqueErr) is wrapped with this message. It represents an unexpected database failure while creating the user row.","triggerScenarios":"The INSERT INTO users statement fails for non-duplicate reasons: connection failure, schema mismatch (missing column), constraint violation other than email uniqueness, or transaction already aborted inside the WithTx block in CreateAccount.","commonSituations":"DB migrations not applied (columns missing), database unreachable during deploy, transaction rolled back earlier leaving the tx in an aborted state, or a different unique index (e.g. domain) being violated.","solutions":["Log the wrapped underlying error (%v of err) to see the actual pq driver failure","Verify the users table schema matches the INSERT (run pending migrations)","Check DB connectivity and that the transaction passed to CreateUser is still valid/alive","Confirm which constraint failed — a non-email unique index still lands in this branch","Retry the request if the failure was transient (connection blip)"],"exampleFix":"// before\nreturn nil, fmt.Errorf(\"error creating user: %v\", err)\n// after\nreturn nil, fmt.Errorf(\"error creating user: %w\", err) // enables errors.Is/As on the pq.Error","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"user, err := db.CreateUser(name, email, tx)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"error creating user\") {\n        log.Printf(\"user insert failed: %v\", err) // inspect wrapped pq error\n        return http.StatusBadGateway // or retry if transient\n    }\n}","preventionTips":["Keep migrations applied so the users table schema matches the INSERT","Use %w wrapping (and errors.As with *pq.Error) to classify failures","Ensure the sqlx.Tx is alive and not already aborted before calling","Alert on elevated rates of this error — usually infra/schema, not user input"],"tags":["go","database","sql"],"backgroundTag":"database-insert-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"}