different-ai/openwork · error
Failed to switch organization (${response.status}).
Error message
Failed to switch organization (${response.status}). What it means
setActiveOrganization POSTs the chosen organizationId/slug and throws this error when the response is not ok. The active-organization switch on the server did not persist, so subsequent org-scoped calls may still target the previous organization. The message carries the HTTP status unless the payload contains a server error message.
Source
Thrown at ee/apps/den-web/app/(den)/dashboard/_providers/org-dashboard-provider.tsx:170
if (!response.ok) {
throw new Error(getErrorMessage(payload, `Failed to load organizations (${response.status}).`));
}
return parseOrgListPayload(payload);
}
async function setActiveOrganization(input: { organizationId?: string | null; organizationSlug?: string | null }) {
const { response, payload } = await requestJson(
"/api/auth/organization/set-active",
{
method: "POST",
body: JSON.stringify(input),
},
12000,
);
if (!response.ok) {
throw new Error(getErrorMessage(payload, `Failed to switch organization (${response.status}).`));
}
}
async function loadOrgContext(organizationId: string, refreshRoles: boolean) {
const path = refreshRoles ? "/v1/org?refreshRoles=true" : "/v1/org";
const { response, payload } = await requestJson(
path,
{ method: "GET", headers: { [ORG_SCOPE_HEADER]: organizationId } },
12000,
);
if (!response.ok) {
if (response.status === 404) {
throw new OrganizationNotFoundError(getErrorMessage(payload, `Failed to load organization (${response.status}).`));
}
throw new Error(getErrorMessage(payload, `Failed to load organization (${response.status}).`));
}
View on GitHub (pinned to 2b7df46e8a)
Solutions
- Read the status: 404 → clear the stored organizationId/slug (localStorage/cookie) and fall back to the default org from loadOrgDirectory.
- 403 → verify the user's membership/role in the target org on the server before offering the switch in the UI.
- 401 → redirect the user to sign-in; the session cookie has expired.
- 4xx/5xx with a payload message → surface getErrorMessage output to the user instead of the generic message.
- After a failed restore, call refreshOrgData() to resync the directory instead of retrying the stale org.
Example fix
// before
await restoreDisplayedOrganization(); // throws on deleted org
// after
try {
await restoreDisplayedOrganization();
} catch (err) {
clearStoredOrganization();
await refreshOrgData(); // fall back to first available org
} Defensive patterns
Strategy: fallback
Validate before calling
// verify membership before switching
const orgs = await loadOrgDirectory();
if (!orgs.some(o => o.slug === targetSlug || o.id === targetId)) throw new Error("Not a member of target org"); Type guard
function isSwitchableOrg(org: unknown): org is { id: string; slug: string } {
return typeof org === "object" && org !== null && typeof (org as { id?: unknown }).id === "string" && typeof (org as { slug?: unknown }).slug === "string";
} Try / catch
try {
await setActiveOrganization({ organizationSlug: slug });
} catch (err) {
clearStoredOrganization();
await refreshOrgData(); // fall back to a valid default org
} Prevention
- Validate the target org against the directory before switching.
- Purge stored org ids/slugs on sign-out and on 404.
- After renames, resync slugs from the server rather than trusting cache.
- Handle 403 by hiding orgs the user can't access in the switcher UI.
- Catch restore failures at startup so a deleted org never blocks the dashboard.
When it happens
Trigger: POST switch endpoint returns 401 (session expired), 403 (no membership in the target org), 404 (organizationId/slug no longer exists or was deleted), 409/422 (org suspended or payload rejected), or 5xx from server faults.
Common situations: Switching to an org the user was just removed from; restoreDisplayedOrganization attempting to restore an org deleted in another tab; stale cached slug after an org rename.
Understand the failure class
Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.
Related errors
- Failed to load organizations (${response.status}).
- Failed to load organization (${response.status}).
- Failed to create organization (${response.status}).
- Failed to create skill (${response.status}).
- Failed to save skill (${response.status}).
AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01).
Data as JSON: /api/errors/359047cbf9d40a3d.
Report an issue: GitHub.