{"record":{"id":"f090ad25e9e5454c","repo":"plandex-ai/plandex","slug":"error-validating-org-membership-v","errorCode":null,"errorMessage":"error validating org membership: %v","messagePattern":"error validating org membership: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/org_helpers.go","lineNumber":90,"sourceCode":"\terr := Conn.Get(&org, query, orgId)\n\n\tif err != nil {\n\t\tif err == sql.ErrNoRows {\n\t\t\treturn nil, fmt.Errorf(\"org not found\")\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"error getting org: %v\", err)\n\t}\n\n\treturn &org, nil\n}\n\nfunc ValidateOrgMembership(userId string, orgId string) (bool, error) {\n\tvar count int\n\terr := Conn.QueryRow(\"SELECT COUNT(*) FROM orgs_users WHERE user_id = $1 AND org_id = $2\", userId, orgId).Scan(&count)\n\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"error validating org membership: %v\", err)\n\t}\n\n\treturn count > 0, nil\n}\n\nfunc CreateOrg(req *shared.CreateOrgRequest, userId string, domain *string, tx *sqlx.Tx) (*Org, error) {\n\torg := &Org{\n\t\tName:               req.Name,\n\t\tDomain:             domain,\n\t\tAutoAddDomainUsers: req.AutoAddDomainUsers,\n\t\tOwnerId:            userId,\n\t}\n\n\terr := tx.QueryRow(\"INSERT INTO orgs (name, domain, auto_add_domain_users, owner_id, is_trial) VALUES ($1, $2, $3, $4, false) RETURNING id\", req.Name, domain, req.AutoAddDomainUsers, userId).Scan(&org.Id)\n\n\tif err != nil {\n\t\tif IsNonUniqueErr(err) {\n\t\t\t// Handle the uniqueness constraint violation","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/org_helpers.go#L72-L108","documentation":"ValidateOrgMembership wraps any error from the COUNT(*) query against orgs_users into 'error validating org membership: %v'. This means the membership check could not be performed at all — it is a database-layer failure (connectivity, bad SQL, scan error), not a 'user is not a member' result. The boolean false return paired with the error must not be interpreted as a definitive denial.","triggerScenarios":"Any call to ValidateOrgMembership when the Postgres query fails: DB connection dropped/pool exhausted, orgs_users table missing, or Conn.QueryRow(...).Scan failing on a row error. Called from execAuthenticate, InviteUserHandler and DeleteOrgUserHandler.","commonSituations":"Postgres is down or restarting during deploy; connection pool saturated under load; schema migrations not applied so orgs_users does not exist; using the function with a stale Conn handle after a connection reset.","solutions":["Check the wrapped %v error for a pq/lib/pq connection failure and verify the Postgres instance is reachable from the server","Run schema migrations to ensure the orgs_users table and columns exist","Verify DATABASE_URL / Conn initialization and connection-pool limits","Retry the request once the database is healthy; the error is transient in most cases"],"exampleFix":"// before\nok, err := db.ValidateOrgMembership(userId, orgId)\nif err != nil { /* treat as not-member */ }\n// after\nok, err := db.ValidateOrgMembership(userId, orgId)\nif err != nil {\n    log.Printf(\"membership check failed (db error): %v\", err)\n    http.Error(w, \"internal error\", http.StatusInternalServerError)\n    return\n}","handlingStrategy":"try-catch","validationCode":"// Go has no pre-call check; validate inputs are non-empty UUIDs before calling\nif userId == \"\" || orgId == \"\" { return false, errors.New(\"userId and orgId required\") }","typeGuard":"// Distinguish db-layer failure from definitive 'not a member'\nfunc isDBErr(err error) bool { return err != nil && !strings.Contains(err.Error(), \"no rows\") }","tryCatchPattern":"ok, err := db.ValidateOrgMembership(userId, orgId)\nif err != nil {\n    log.Printf(\"org membership validation failed: %v\", err)\n    return fmt.Errorf(\"membership check unavailable, try again\")\n}\nif !ok { /* definitively not a member */ }","preventionTips":["Never treat the error return as 'not a member' — only a nil error with false means that","Monitor Postgres health/alerts; this error usually appears during outages","Keep migrations applied so orgs_users always exists","Set sane connection-pool limits and retries on the DB handle"],"tags":["database","postgresql","org-membership"],"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"}