Mintplex-Labs/anything-llm · error

${res.statusText || "Error generating api key."}

Error message

${res.statusText || "Error generating api key."}

What it means

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.

Source

Thrown at frontend/src/models/admin.js:216

        if (!res.ok) {
          throw new Error(res.statusText || "Error fetching api keys.");
        }
        return res.json();
      })
      .catch((e) => {
        console.error(e);
        return { apiKeys: [], error: e.message };
      });
  },
  generateApiKey: async function (data = {}) {
    return fetch(`${API_BASE}/admin/generate-api-key`, {
      method: "POST",
      headers: baseHeaders(),
      body: JSON.stringify(data),
    })
      .then((res) => {
        if (!res.ok) {
          throw new Error(res.statusText || "Error generating api key.");
        }
        return res.json();
      })
      .catch((e) => {
        console.error(e);
        return { apiKey: null, error: e.message };
      });
  },
  deleteApiKey: async function (apiKeyId = "") {
    return fetch(`${API_BASE}/admin/delete-api-key/${apiKeyId}`, {
      method: "DELETE",
      headers: baseHeaders(),
    })
      .then((res) => res.ok)
      .catch((e) => {
        console.error(e);
        return false;
      });

View on GitHub (pinned to 526360e320)

Solutions

  1. Validate the form payload client-side before posting.
  2. On 401, re-authenticate; on 403, confirm admin role.
  3. Check backend logs for the 400/500 detail — the generic message hides it.
  4. Improve the throw to read the JSON error body (the current code discards server-side error text).

Example fix

// before
if (!res.ok) throw new Error(res.statusText || 'Error generating api key.');

// after
if (!res.ok) {
  const body = await res.json().catch(() => ({}));
  throw new Error(body?.error || `Error generating api key (HTTP ${res.status})`);
}
Defensive patterns

Strategy: validation

Validate before calling

function validateApiKeyPayload(data) {
  if (!data || typeof data !== 'object') throw new Error('API key payload is required');
  return data;
}

Type guard

function isAuthFailure(e) { return /401|unauthor|Forbidden|403/i.test(e?.message || ''); }

Try / catch

const { apiKey, error } = await Admin.generateApiKey(payload);
if (error) {
  if (isAuthFailure({ message: error })) { await redirectToLogin(); return; }
  showToast(error);
}

Prevention

When it happens

Trigger: 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.

Common situations: Form submitted with empty fields; session expired between opening the modal and clicking generate; DB locked or migration pending on the backend.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/7af5383197c9db11. Report an issue: GitHub.