plandex-ai/plandex · error

error converting orgs to api orgs: %v

Error message

error converting orgs to api orgs: %v

What it means

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.

Source

Thrown at app/server/handlers/auth_helpers.go:427

	// with a single org, set the orgId in the cookie
	// otherwise, the user will be prompted to select an org
	var orgId string
	if len(orgs) == 1 {
		orgId = orgs[0].Id
	}

	log.Println("Setting auth cookie if browser")
	err = SetAuthCookieIfBrowser(w, r, user, token, orgId)
	if err != nil {
		log.Printf("Error setting auth cookie: %v\n", err)
		return nil, fmt.Errorf("error setting auth cookie: %v", err)
	}

	apiOrgs, apiErr := toApiOrgs(orgs)

	if apiErr != nil {
		log.Printf("Error converting orgs to api orgs: %v\n", apiErr)
		return nil, fmt.Errorf("error converting orgs to api orgs: %v", apiErr)
	}

	resp := shared.SessionResponse{
		UserId:      user.Id,
		Token:       token,
		Email:       user.Email,
		UserName:    user.Name,
		Orgs:        apiOrgs,
		IsLocalMode: os.Getenv("GOENV") == "development" && os.Getenv("LOCAL_MODE") == "1",
	}

	return &resp, nil
}

func requireMinClientVersion(w http.ResponseWriter, r *http.Request, minVersion string) bool {
	msg := fmt.Sprintf("Client version is too old for this endpoint. Please upgrade to version %s or later.", minVersion)

	version := r.Header.Get("X-Client-Version")

View on GitHub (pinned to e2d772072e)

Solutions

  1. Inspect the wrapped apiErr from toApiOrgs for the specific conversion failure
  2. Check the user's org memberships for orphaned/deleted org references
  3. Re-run or repair migrations if org schema drifted from API structs
  4. Add validation/logging in toApiOrgs to identify the offending org record

Example fix

// before
if apiErr != nil {
	return nil, fmt.Errorf("error converting orgs to api orgs: %v", apiErr)
}
// after
if apiErr != nil {
	log.Printf("toApiOrgs failed orgs=%+v: %v", orgs, apiErr)
	return nil, fmt.Errorf("error converting orgs to api orgs: %v", apiErr)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity-check org data before building the response
for _, o := range orgs {
	if o == nil || o.Id == "" {
		return fmt.Errorf("invalid org record in memberships")
	}
}

Type guard

func orgsConvertible(orgs []*db.Org) bool {
	for _, o := range orgs {
		if o == nil || o.Id == "" {
			return false
		}
	}
	return true
}

Try / catch

resp, err := ValidateAndSignIn(w, r, req)
if err != nil {
	if strings.HasPrefix(err.Error(), "error converting orgs to api orgs") {
		http.Error(w, "session data malformed; contact support", http.StatusInternalServerError)
		return
	}
	http.Error(w, err.Error(), http.StatusUnauthorized)
}

Prevention

When it happens

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

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

Related errors


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