{"record":{"id":"7af5383197c9db11","repo":"Mintplex-Labs/anything-llm","slug":"res-statustext-error-generating-api-key","errorCode":null,"errorMessage":"${res.statusText || \"Error generating api key.\"}","messagePattern":"\\$\\{res\\.statusText \\|\\| \"Error generating api key\\.\"\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/models/admin.js","lineNumber":216,"sourceCode":"        if (!res.ok) {\n          throw new Error(res.statusText || \"Error fetching api keys.\");\n        }\n        return res.json();\n      })\n      .catch((e) => {\n        console.error(e);\n        return { apiKeys: [], error: e.message };\n      });\n  },\n  generateApiKey: async function (data = {}) {\n    return fetch(`${API_BASE}/admin/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}/admin/delete-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":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/frontend/src/models/admin.js#L198-L234","documentation":"Thrown from `generateApiKey` when `POST /api/admin/generate-api-key` returns non-ok. Same shape as error 49: message is `res.statusText || 'Error generating api key.'`, caught and returned as `{ apiKey: null, error }`. Generation is a write, so 4xx validation errors are more likely here than on the GET.","triggerScenarios":"Request body missing required fields (e.g. created_by, permissions); 401/403 non-admin; 409/400 if a key with the same name exists; backend persistence failure (DB write error) returns 500; rate limiting.","commonSituations":"Form submitted with empty fields; session expired between opening the modal and clicking generate; DB locked or migration pending on the backend.","solutions":["Validate the form payload client-side before posting.","On 401, re-authenticate; on 403, confirm admin role.","Check backend logs for the 400/500 detail — the generic message hides it.","Improve the throw to read the JSON error body (the current code discards server-side error text)."],"exampleFix":"// before\nif (!res.ok) throw new Error(res.statusText || 'Error generating api key.');\n\n// after\nif (!res.ok) {\n  const body = await res.json().catch(() => ({}));\n  throw new Error(body?.error || `Error generating api key (HTTP ${res.status})`);\n}","handlingStrategy":"validation","validationCode":"function validateApiKeyPayload(data) {\n  if (!data || typeof data !== 'object') throw new Error('API key payload is required');\n  return data;\n}","typeGuard":"function isAuthFailure(e) { return /401|unauthor|Forbidden|403/i.test(e?.message || ''); }","tryCatchPattern":"const { apiKey, error } = await Admin.generateApiKey(payload);\nif (error) {\n  if (isAuthFailure({ message: error })) { await redirectToLogin(); return; }\n  showToast(error);\n}","preventionTips":["Validate the generate form client-side before posting.","Re-auth on 401; check backend logs for 400/500 detail.","Patch the model to read the JSON error body for the real message."],"tags":["frontend","api-keys","admin","network","fetch","validation"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}