{"record":{"id":"47386ac1ef2fac13","repo":"supabase/supabase","slug":"projectref-is-required-47386a","errorCode":null,"errorMessage":"projectRef is required","messagePattern":"projectRef is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/studio/data/network-restrictions/network-restrictions-query.ts","lineNumber":21,"sourceCode":"import { networkRestrictionKeys } from './keys'\nimport { get, handleError } from '@/data/fetchers'\nimport { UseCustomQueryOptions } from '@/types'\n\nexport type NetworkRestrictionsVariables = { projectRef?: string }\n\nexport type NetworkRestrictionsResponse = {\n  entitlement: 'disallowed' | 'allowed'\n  status: '' | 'stored' | 'applied'\n  config: { dbAllowedCidrs: string[] }\n  old_config?: { dbAllowedCidrs: string[] }\n  error?: any\n}\n\nexport async function getNetworkRestrictions(\n  { projectRef }: NetworkRestrictionsVariables,\n  signal?: AbortSignal\n) {\n  if (!projectRef) throw new Error('projectRef is required')\n\n  const { data, error } = await get('/v1/projects/{ref}/network-restrictions', {\n    params: { path: { ref: projectRef } },\n    signal,\n  })\n\n  // Not allowed error is a valid response to denote if a project\n  // has access to the network restrictions UI, so we'll handle it here\n  if (error) {\n    const isNotAllowedError =\n      (error as any)?.code === 400 &&\n      (error as any)?.message?.includes('not allowed to set up network restrictions')\n\n    if (isNotAllowedError) {\n      return {\n        entitlement: 'disallowed',\n        config: { dbAllowedCidrs: [] },\n        status: '',","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/supabase/supabase/blob/beee91b9c2228dd57302dec75c733baaa84ab543/apps/studio/data/network-restrictions/network-restrictions-query.ts#L3-L39","documentation":"Thrown by getNetworkRestrictions when projectRef is missing. The function GETs /v1/projects/{ref}/network-restrictions and the path param is the project's ref string; an empty ref would either 404 or hit the wrong project, so the guard refuses to call the API.","triggerScenarios":"useNetworkRestrictionsQuery runs before the project route param resolves (e.g. ref is still undefined on first render), or a component is mounted outside a project route so useParams returns undefined for ref.","commonSituations":"Settings page rendered during project switch; deep-linked settings tab loading before the project store hydrates; tests that mount the component without a projectRef in the wrapper.","solutions":["Gate the query: enabled: !!projectRef on the useQuery options.","Pull projectRef from the project store / useParams and only render the NetworkRestrictions UI when it is truthy.","In tests, pass a fixed projectRef via the query hook's variables."],"exampleFix":"// before\nexport const useNetworkRestrictionsQuery = ({ projectRef }) =>\n  useQuery({\n    queryKey: networkRestrictionKeys.list(projectRef),\n    queryFn: ({ signal }) => getNetworkRestrictions({ projectRef }, signal),\n  })\n\n// after\nexport const useNetworkRestrictionsQuery = ({ projectRef }) =>\n  useQuery({\n    queryKey: networkRestrictionKeys.list(projectRef),\n    enabled: Boolean(projectRef),\n    queryFn: ({ signal }) => getNetworkRestrictions({ projectRef: projectRef! }, signal),\n  })","handlingStrategy":"validation","validationCode":"// gate the React Query so the fetcher never runs without a ref\nexport const useNetworkRestrictionsQuery = ({ projectRef }: { projectRef?: string }) =>\n  useQuery({\n    queryKey: networkRestrictionKeys.list(projectRef),\n    enabled: Boolean(projectRef),\n    queryFn: ({ signal }) => getNetworkRestrictions({ projectRef: projectRef! }, signal),\n  })","typeGuard":"const hasProjectRef = (\n  v: { projectRef?: string }\n): v is { projectRef: string } =>\n  typeof v.projectRef === 'string' && v.projectRef.length > 0","tryCatchPattern":"const { error } = useNetworkRestrictionsQuery({ projectRef })\n\nif (error instanceof Error && error.message === 'projectRef is required') {\n  // project context not loaded yet; render a skeleton, do not refetch\n}","preventionTips":["Treat projectRef as a render precondition for any project-scoped settings page.","Use enabled: !!projectRef on every project-scoped query to keep the guard unreachable in practice.","Pull projectRef from a single source (route param or project store) and thread it explicitly."],"tags":["validation","precondition","project-ref","network-restrictions","react-query"],"backgroundTag":null,"analyzedSha":"beee91b9c2228dd57302dec75c733baaa84ab543","analyzedAt":"2026-08-12T06:51:48.935Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}