{"record":{"id":"5e221313ace87ef8","repo":"iflytek/astron-agent","slug":"use-space-type","errorCode":null,"errorMessage":"最近访问空间不存在","messagePattern":"最近访问空间不存在","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"console/frontend/src/hooks/use-space-type.ts","lineNumber":248,"sourceCode":"        break;\n      case SPACE_TYPES.TEAM:\n        navigate?.(`/enterprise/${enterpriseId}/space`);\n        break;\n      default:\n        console.warn('Unknown space type:', spaceType);\n        break;\n    }\n  }, [spaceType, navigate, enterpriseId]);\n\n  const getLastVisitSpaceInfo = useCallback(\n    async (cb?: () => void) => {\n      try {\n        const spaceData: any = await getLastVisitSpace();\n        const { id, enterpriseId, name, avatarUrl } = spaceData || {};\n\n        if (!id) {\n          if (!enterpriseId) {\n            throw new Error('最近访问空间不存在');\n          }\n\n          if (!window.location.pathname.includes('/home')) {\n            window.location.href = '/home';\n            return;\n          }\n\n          handleTeamSwitch(enterpriseId);\n          return;\n        }\n\n        setSpaceStore({\n          spaceType: enterpriseId ? 'team' : 'personal',\n          spaceId: id,\n          spaceName: name,\n          spaceAvatar: avatarUrl,\n          enterpriseId: `${enterpriseId || ''}`,\n        });","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/frontend/src/hooks/use-space-type.ts#L230-L266","documentation":"useSpaceType fetches the user's last-visited space via getLastVisitSpace(). If the API returns data with neither a space id nor an enterpriseId, the hook throws '最近访问空间不存在' (the last visited space does not exist), meaning the backend has no recorded space for this user and no enterprise fallback is possible.","triggerScenarios":"Calling getLastVisitSpace() for a brand-new user who has never visited any space; the last-visited space record was deleted (space removed) and the user belongs to no enterprise; backend returns an empty/null payload after login or token refresh.","commonSituations":"Fresh accounts landing on the console before any space is created; stale localStorage/session pointing at a deleted space; test environments with wiped databases; multi-tenant setups where the user was removed from their enterprise.","solutions":["Verify the user actually has at least one space or enterprise membership in the backend; create a default space on first login.","Wrap getLastVisitSpace() in try/catch and redirect to /home (space-selection/onboarding page) instead of throwing.","Check that the auth token/space-id headers sent by the request interceptor are correct for this user.","Clear stale client-side space state (localStorage/recoil persistence) and re-fetch."],"exampleFix":"// before\nconst spaceData: any = await getLastVisitSpace();\nconst { id, enterpriseId } = spaceData || {};\nif (!id && !enterpriseId) throw new Error('最近访问空间不存在');\n// after\nlet spaceData: any = null;\ntry {\n  spaceData = await getLastVisitSpace();\n} catch { spaceData = null; }\nconst { id, enterpriseId } = spaceData || {};\nif (!id && !enterpriseId) {\n  window.location.href = '/home';\n  return;\n}","handlingStrategy":"fallback","validationCode":"// guard before rendering components that need a space\nconst hasSpace = !!(spaceData?.id || spaceData?.enterpriseId);\nif (!hasSpace) window.location.href = '/home';","typeGuard":"function hasValidSpace(d: any): d is { id: string; enterpriseId?: string } {\n  return !!d && typeof d.id === 'string' && d.id.length > 0;\n}","tryCatchPattern":"try {\n  const spaceData = await getLastVisitSpace();\n  // ...\n} catch {\n  window.location.href = '/home'; // onboarding / space selection\n}","preventionTips":["Provision a default personal space on first login","Clear stale persisted space state when a space/enterprise is deleted","Verify user-enterprise membership during account setup in test environments","Always provide a /home fallback path when space lookup fails"],"tags":["react-hooks","multi-tenancy","space-not-found","onboarding"],"backgroundTag":"record-not-found","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}