gitroomhq/postiz-app · warning · Error

Failed to load stats

Error message

Failed to load stats

What it means

The admin stats component's useStats SWR hook fetches /admin/stats and throws 'Failed to load stats' on any non-ok response. Same generic-wrap pattern as the errors hook; root cause is the HTTP status, typically 401/403 auth or a backend error.

Source

Thrown at apps/frontend/src/components/admin/admin-stats.component.tsx:79

const useStats = (params: {
  from: string;
  to: string;
  unknownOnly: boolean;
}) => {
  const fetch = useFetch();
  const query = new URLSearchParams({
    from: params.from,
    to: params.to,
    ...(params.unknownOnly ? { unknownOnly: 'true' } : {}),
  });
  const key = `/admin/stats?${query.toString()}`;
  return useSWR<StatsResponse>(
    key,
    async (url: string) => {
      const res = await fetch(url);
      if (!res.ok) {
        throw new Error('Failed to load stats');
      }
      return res.json();
    },
    {
      revalidateOnFocus: false,
      revalidateOnReconnect: false,
    }
  );
};

const SummaryCard: FC<{ label: string; value: number }> = ({
  label,
  value,
}) => (
  <div className="border border-newTableBorder rounded-[8px] p-[16px] bg-newBgColorInner">
    <div className="text-[12px] opacity-70">{label}</div>
    <div className="text-[28px] font-[600]">{value.toLocaleString()}</div>
  </div>

View on GitHub (pinned to 0f1647f749)

Solutions

  1. Check the network tab for the real status on /admin/stats
  2. Re-authenticate if 401; verify admin role if 403
  3. Inspect backend logs if 500 — often a failing stats aggregation query
  4. Include status/body in the error message and show a session-expired toast for 401

Example fix

// before
if (!res.ok) throw new Error('Failed to load stats');

// after
if (!res.ok) {
  if (res.status === 401) throw new Error('Session expired — please sign in again');
  throw new Error(`Failed to load stats (${res.status})`);
}
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

const { data, error } = useStats();
if (error) showBanner(error.status === 401 ? 'Session expired' : 'Stats unavailable');

Prevention

When it happens

Trigger: Non-admin or unauthenticated user loading the admin dashboard; expired session token; backend route error (500) while computing stats; query params producing a validation 400.

Common situations: Leaving the admin tab open past session expiry (SWR revalidates on focus and gets 401); role misconfiguration; backend migration breaking the stats aggregation query.

Related errors


AI-assisted analysis of gitroomhq/postiz-app@0f1647f749 (2026-08-27). Data as JSON: /api/errors/cb3f477ab3b4b231. Report an issue: GitHub.