{"record":{"id":"bb91cf4ca1749b7d","repo":"Mintplex-Labs/anything-llm","slug":"res-statustext-error-generating-api-key-bb91cf","errorCode":null,"errorMessage":"res.statusText || \"Error generating api key.\"","messagePattern":"res\\.statusText \\|\\| \"Error generating api key\\.\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/src/models/system.js","lineNumber":570,"sourceCode":"        if (!res.ok) {\n          throw new Error(res.statusText || \"Error fetching api key.\");\n        }\n        return res.json();\n      })\n      .catch((e) => {\n        console.error(e);\n        return { apiKey: null, error: e.message };\n      });\n  },\n  generateApiKey: async function (data = {}) {\n    return fetch(`${API_BASE}/system/generate-api-key`, {\n      method: \"POST\",\n      headers: baseHeaders(),\n      body: JSON.stringify(data),\n    })\n      .then((res) => {\n        if (!res.ok) {\n          throw new Error(res.statusText || \"Error generating api key.\");\n        }\n        return res.json();\n      })\n      .catch((e) => {\n        console.error(e);\n        return { apiKey: null, error: e.message };\n      });\n  },\n  deleteApiKey: async function (apiKeyId = \"\") {\n    return fetch(`${API_BASE}/system/api-key/${apiKeyId}`, {\n      method: \"DELETE\",\n      headers: baseHeaders(),\n    })\n      .then((res) => res.ok)\n      .catch((e) => {\n        console.error(e);\n        return false;\n      });","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/frontend/src/models/system.js#L552-L588","documentation":"Thrown by the frontend System API client when POST /system/generate-api-key returns a non-2xx HTTP status. The message uses res.statusText (the HTTP reason phrase) and only falls back to 'Error generating api key.' when the status text is empty (e.g. HTTP/2 responses or synthesized network errors). The surrounding .catch swallows the throw and returns { apiKey: null, error } so callers see a typed failure object, not a thrown exception.","triggerScenarios":"Calling System.generateApiKey(data) while unauthenticated (401/403), when the backend LLM provider key validation rejects the request (422), when the server errors during key generation (500), or when the network/CORS preflight fails (statusText often empty).","commonSituations":"Session expired while the user sat on the API-key settings page; the backend has no LLM API key configured; reverse proxy strips the reason phrase; HTTP/2 server returns no statusText so only the fallback message surfaces.","solutions":["Check the browser DevTools Network tab for the actual status code and server response body on the /system/generate-api-key request.","If 401/403, refresh the session or re-authenticate before regenerating the key.","If 500, inspect the backend server logs around the generate-api-key controller for the real exception.","Render both res.status and the response body in the thrown message so the fallback is no longer information-lossy."],"exampleFix":"// before\nif (!res.ok) {\n  throw new Error(res.statusText || \"Error generating api key.\");\n}\n\n// after\nif (!res.ok) {\n  let detail = res.statusText;\n  try { const body = await res.json(); detail = body?.message || detail; } catch {}\n  throw new Error(detail || `Error generating api key (HTTP ${res.status}).`);\n}","handlingStrategy":"try-catch","validationCode":"// Confirm session is authed and the endpoint is reachable before generating.\nasync function canGenerate() {\n  const r = await fetch(`${API_BASE}/system/ping`, { headers: baseHeaders() });\n  return r.ok;\n}","typeGuard":"// Result type guard for the generateApiKey return shape.\nfunction isApiKeyFailure(x): x is { apiKey: null; error: string } {\n  return x && x.apiKey === null && typeof x.error === 'string';\n}","tryCatchPattern":"const result = await System.generateApiKey(payload);\nif (result.apiKey === null) {\n  showToast(result.error || 'Could not generate API key.');\n  return;\n}\n// use result.apiKey","preventionTips":["Always destructure { apiKey, error } and branch on apiKey === null rather than relying on thrown exceptions.","Surface result.error verbatim in the UI — it carries the server reason phrase.","Refresh the auth session before invoking if the user has been idle."],"tags":["frontend","api-client","http","authentication","network"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}