iflytek/astron-agent · error

最近访问空间不存在

Error message

最近访问空间不存在

What it means

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.

Solutions

  1. Verify the user actually has at least one space or enterprise membership in the backend; create a default space on first login.
  2. Wrap getLastVisitSpace() in try/catch and redirect to /home (space-selection/onboarding page) instead of throwing.
  3. Check that the auth token/space-id headers sent by the request interceptor are correct for this user.
  4. Clear stale client-side space state (localStorage/recoil persistence) and re-fetch.

Example fix

// before
const spaceData: any = await getLastVisitSpace();
const { id, enterpriseId } = spaceData || {};
if (!id && !enterpriseId) throw new Error('最近访问空间不存在');
// after
let spaceData: any = null;
try {
  spaceData = await getLastVisitSpace();
} catch { spaceData = null; }
const { id, enterpriseId } = spaceData || {};
if (!id && !enterpriseId) {
  window.location.href = '/home';
  return;
}
Defensive patterns

Strategy: fallback

Validate before calling

// guard before rendering components that need a space
const hasSpace = !!(spaceData?.id || spaceData?.enterpriseId);
if (!hasSpace) window.location.href = '/home';

Type guard

function hasValidSpace(d: any): d is { id: string; enterpriseId?: string } {
  return !!d && typeof d.id === 'string' && d.id.length > 0;
}

Try / catch

try {
  const spaceData = await getLastVisitSpace();
  // ...
} catch {
  window.location.href = '/home'; // onboarding / space selection
}

Prevention

When it happens

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

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

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/5e221313ace87ef8. Report an issue: GitHub.

Appendix: source

Thrown at console/frontend/src/hooks/use-space-type.ts:248

        break;
      case SPACE_TYPES.TEAM:
        navigate?.(`/enterprise/${enterpriseId}/space`);
        break;
      default:
        console.warn('Unknown space type:', spaceType);
        break;
    }
  }, [spaceType, navigate, enterpriseId]);

  const getLastVisitSpaceInfo = useCallback(
    async (cb?: () => void) => {
      try {
        const spaceData: any = await getLastVisitSpace();
        const { id, enterpriseId, name, avatarUrl } = spaceData || {};

        if (!id) {
          if (!enterpriseId) {
            throw new Error('最近访问空间不存在');
          }

          if (!window.location.pathname.includes('/home')) {
            window.location.href = '/home';
            return;
          }

          handleTeamSwitch(enterpriseId);
          return;
        }

        setSpaceStore({
          spaceType: enterpriseId ? 'team' : 'personal',
          spaceId: id,
          spaceName: name,
          spaceAvatar: avatarUrl,
          enterpriseId: `${enterpriseId || ''}`,
        });

View on GitHub (pinned to 5e758547a8)