{"record":{"id":"0072aae062516586","repo":"DayuanJiang/next-ai-draw-io","slug":"request-failed-res-status","errorCode":null,"errorMessage":"Request failed (${res.status})","messagePattern":"Request failed \\((.+?)\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/[lang]/admin/admin-shared.ts","lineNumber":62,"sourceCode":"export interface EnvProvider {\n    name: string\n    provider: ProviderName\n    models: string[]\n    isDefault: boolean\n}\n\nexport async function adminFetch(path: string, pw: string, init?: RequestInit) {\n    const res = await fetch(getApiEndpoint(path), {\n        ...init,\n        headers: {\n            ...init?.headers,\n            \"x-admin-password\": pw,\n            ...(init?.body ? { \"Content-Type\": \"application/json\" } : {}),\n        },\n    })\n    const data = await res.json().catch(() => ({}))\n    if (!res.ok) {\n        throw new Error(data.error || `Request failed (${res.status})`)\n    }\n    return data\n}\n","sourceCodeStart":44,"sourceCodeEnd":66,"githubUrl":"https://github.com/DayuanJiang/next-ai-draw-io/blob/155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1/app/[lang]/admin/admin-shared.ts#L44-L66","documentation":"Generic HTTP failure raised by adminFetch when the server responds with a non-2xx status and the response body contains no `error` field. It is a catch-all wrapper for all admin API calls (settings, providers data fetching) in the admin panel.","triggerScenarios":"Any admin page fetch (data/[settingsData, providersData] hooks) hitting an admin route that returns 4xx/5xx without a JSON error body, e.g. 401 wrong admin password, 500 server crash, or a route returning plain text.","commonSituations":"Wrong or missing x-admin-password header, expired admin session, Next.js server not fully started, or an admin API route throwing before returning structured JSON.","solutions":["Verify ADMIN_PASSWORD is set and the admin password entered matches it","Check the server logs for the underlying route error behind the non-2xx status","Retry after the dev/prod server is fully compiled and running","Inspect the raw response (curl with the x-admin-password header) to see body/status detail"],"exampleFix":"// before\nconst data = await adminFetch('/api/admin/settings', pw)\n\n// after\nconst data = await adminFetch('/api/admin/settings', pw).catch((e) => {\n  console.error('admin fetch failed:', e.message)\n  return null\n})\nif (!data) return <p>Failed to load settings — check admin password/server logs.</p>","handlingStrategy":"try-catch","validationCode":"const pw = getAdminPassword()\nif (!pw) throw new Error('Admin password required before fetching')","typeGuard":null,"tryCatchPattern":"try {\n  const data = await adminFetch(url, pw, init)\n} catch (e) {\n  // e.message is either server 'error' field or 'Request failed (status)'\n  if (/\\(401\\)|\\(403\\)/.test((e as Error).message)) redirect('/admin/login')\n  else showError((e as Error).message)\n}","preventionTips":["Ensure ADMIN_PASSWORD env is configured before using admin pages","Check server health before batch admin fetches","Log the status code from the message to route 401s to re-auth"],"tags":["admin","http","fetch","api"],"backgroundTag":"http-request-failed","analyzedSha":"155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1","analyzedAt":"2026-08-27T11:40:38.297Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}