mastra-ai/mastra · error

Failed to load Factories

Error message

Failed to load Factories

What it means

`fetchFactoryProjects` wraps `listFactoryProjects`, which returns null instead of throwing on request failure. This error converts that null into an explicit failure so React Query treats it as an error rather than caching an empty result.

Source

Thrown at mastracode/factory-ui/src/hooks/useFactories.ts:25

  createFactoryProject,
  deleteFactoryProject,
  linkRepository,
  listFactoryProjects,
  unlinkRepository,
} from '../ui/domains/workspaces/services/github';
import type { FactoryProject, GithubRepo } from '../ui/domains/workspaces/services/github';

function invalidateFactories(queryClient: ReturnType<typeof useQueryClient>) {
  void queryClient.invalidateQueries({ queryKey: queryKeys.factories() });
}

function refetchFactories(queryClient: ReturnType<typeof useQueryClient>) {
  return queryClient.refetchQueries({ queryKey: queryKeys.factories() });
}

async function fetchFactoryProjects(baseUrl: string): Promise<FactoryProject[]> {
  const projects = await listFactoryProjects(baseUrl);
  if (!projects) throw new Error('Failed to load Factories');
  return projects;
}

export function useFactoriesQuery() {
  const { baseUrl } = useApiConfig();
  return useQuery({
    queryKey: queryKeys.factories(),
    queryFn: () => fetchFactoryProjects(baseUrl),
  });
}

export function useFactoryQuery(factoryId: string | undefined) {
  const { baseUrl } = useApiConfig();
  return useQuery({
    queryKey: queryKeys.factories(),
    queryFn: () => fetchFactoryProjects(baseUrl),
    select: (factories: FactoryProject[]) => factories.find(factory => factory.id === factoryId),
    enabled: Boolean(factoryId),

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check network/server health for the Factories listing endpoint and confirm the server is running
  2. Verify baseUrl and that the authenticated user has access to factory projects
  3. Confirm UI and server versions are compatible (response schema for the projects list)
  4. Add logging inside listFactoryProjects to see why it returns null instead of a list

Example fix

// caller-side
const { data, isError, error } = useFactoriesQuery();
if (isError) {
  showEmptyState('Factories unavailable', error.message); // "Failed to load Factories"
  // offer a retry button calling queryClient.refetchQueries({queryKey: queryKeys.factories()})
}
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight: server reachable and response shaped as a project list
const res = await fetch(`${baseUrl}/factories`, { credentials: 'include' });
if (!res.ok) throw new Error(`Factories endpoint unavailable (${res.status})`);
const body = await res.json();
if (!Array.isArray(body?.projects)) throw new Error('Unexpected Factories payload; check server version');

Type guard

function isFactoryProjectList(value: unknown): value is FactoryProject[] {
  return Array.isArray(value) && value.every(p => typeof p === 'object' && p !== null && 'id' in p);
}

Try / catch

const { data, isError, refetch } = useFactoriesQuery();
if (isError) {
  showRetryBanner('Failed to load Factories', () => refetch());
}

Prevention

When it happens

Trigger: `listFactoryProjects(baseUrl)` returns null/undefined — typically when the underlying fetch to the Factories listing endpoint fails or the payload shape is unexpected.

Common situations: Factory server unreachable or returning non-JSON; user lacks permission to list factory projects; baseUrl misconfigured; response schema changed between UI and server versions so the list parse fails and null is returned.

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


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/9925e29ee89073fb. Report an issue: GitHub.