{"record":{"id":"ffe3551c249c3c79","repo":"plandex-ai/plandex","slug":"error-converting-orgs-to-api-orgs-v","errorCode":null,"errorMessage":"error converting orgs to api orgs: %v","messagePattern":"error converting orgs to api orgs: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/auth_helpers.go","lineNumber":427,"sourceCode":"\t// with a single org, set the orgId in the cookie\n\t// otherwise, the user will be prompted to select an org\n\tvar orgId string\n\tif len(orgs) == 1 {\n\t\torgId = orgs[0].Id\n\t}\n\n\tlog.Println(\"Setting auth cookie if browser\")\n\terr = SetAuthCookieIfBrowser(w, r, user, token, orgId)\n\tif err != nil {\n\t\tlog.Printf(\"Error setting auth cookie: %v\\n\", err)\n\t\treturn nil, fmt.Errorf(\"error setting auth cookie: %v\", err)\n\t}\n\n\tapiOrgs, apiErr := toApiOrgs(orgs)\n\n\tif apiErr != nil {\n\t\tlog.Printf(\"Error converting orgs to api orgs: %v\\n\", apiErr)\n\t\treturn nil, fmt.Errorf(\"error converting orgs to api orgs: %v\", apiErr)\n\t}\n\n\tresp := shared.SessionResponse{\n\t\tUserId:      user.Id,\n\t\tToken:       token,\n\t\tEmail:       user.Email,\n\t\tUserName:    user.Name,\n\t\tOrgs:        apiOrgs,\n\t\tIsLocalMode: os.Getenv(\"GOENV\") == \"development\" && os.Getenv(\"LOCAL_MODE\") == \"1\",\n\t}\n\n\treturn &resp, nil\n}\n\nfunc requireMinClientVersion(w http.ResponseWriter, r *http.Request, minVersion string) bool {\n\tmsg := fmt.Sprintf(\"Client version is too old for this endpoint. Please upgrade to version %s or later.\", minVersion)\n\n\tversion := r.Header.Get(\"X-Client-Version\")","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/auth_helpers.go#L409-L445","documentation":"Wrapped error returned by ValidateAndSignIn when toApiOrgs fails to convert the database org records for the signing-in user into their API representation. It fires after the auth cookie has already been set, during serialization of the org list for the sign-in response — typically because an org record contains data that cannot be mapped to the API org shape. The sign-in flow aborts with a nil user despite successful authentication.","triggerScenarios":"After sign-in, orgs fetched for the user contain data that toApiOrgs cannot map — e.g. malformed org records, nil entries, or an internal conversion error propagated in apiErr.","commonSituations":"Corrupt or partially-migrated org rows; user referencing a deleted org (orphaned membership); schema drift between db models and shared API structs after a version upgrade.","solutions":["Inspect the wrapped apiErr from toApiOrgs for the specific conversion failure","Check the user's org memberships for orphaned/deleted org references","Re-run or repair migrations if org schema drifted from API structs","Add validation/logging in toApiOrgs to identify the offending org record"],"exampleFix":"// before\nif apiErr != nil {\n\treturn nil, fmt.Errorf(\"error converting orgs to api orgs: %v\", apiErr)\n}\n// after\nif apiErr != nil {\n\tlog.Printf(\"toApiOrgs failed orgs=%+v: %v\", orgs, apiErr)\n\treturn nil, fmt.Errorf(\"error converting orgs to api orgs: %v\", apiErr)\n}","handlingStrategy":"try-catch","validationCode":"// sanity-check org data before building the response\nfor _, o := range orgs {\n\tif o == nil || o.Id == \"\" {\n\t\treturn fmt.Errorf(\"invalid org record in memberships\")\n\t}\n}","typeGuard":"func orgsConvertible(orgs []*db.Org) bool {\n\tfor _, o := range orgs {\n\t\tif o == nil || o.Id == \"\" {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}","tryCatchPattern":"resp, err := ValidateAndSignIn(w, r, req)\nif err != nil {\n\tif strings.HasPrefix(err.Error(), \"error converting orgs to api orgs\") {\n\t\thttp.Error(w, \"session data malformed; contact support\", http.StatusInternalServerError)\n\t\treturn\n\t}\n\thttp.Error(w, err.Error(), http.StatusUnauthorized)\n}","preventionTips":["Clean up memberships when orgs are deleted","Keep db models and shared API structs in sync via tests","Add unit tests for toApiOrgs with edge-case org rows","Validate org records on write, not only at serialization time"],"tags":["auth","serialization","orgs"],"backgroundTag":"response-serialization-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"}