{"record":{"id":"9f19dcac90e83db5","repo":"CherryHQ/cherry-studio","slug":"api-request-failed-status-code-response-status","errorCode":null,"errorMessage":"API request failed, status code ${response.status}: ${errorText}","messagePattern":"API request failed, status code (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/ai/mcp/servers/difyKnowledge.ts","lineNumber":147,"sourceCode":"          isError: true\n        }\n      }\n    })\n  }\n\n  private async performListKnowledges(difyKey: string, apiHost: string): Promise<McpResponse> {\n    try {\n      const url = `${apiHost.replace(/\\/$/, '')}/datasets`\n      const response = await net.fetch(url, {\n        method: 'GET',\n        headers: {\n          Authorization: `Bearer ${difyKey}`\n        }\n      })\n\n      if (!response.ok) {\n        const errorText = await response.text()\n        throw new Error(`API request failed, status code ${response.status}: ${errorText}`)\n      }\n\n      const apiResponse = await response.json()\n\n      const knowledges: DifyListKnowledgeResponse[] =\n        apiResponse?.data?.map((item: any) => ({\n          id: item.id,\n          name: item.name,\n          description: item.description || ''\n        })) || []\n\n      const listText =\n        knowledges.length > 0\n          ? knowledges.map((k) => `- **${k.name}** (ID: ${k.id})\\n  ${k.description || 'No Description'}`).join('\\n')\n          : '- No knowledges found.'\n\n      const formattedText = `### Available Knowledge Bases:\\n\\n${listText}`\n","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/difyKnowledge.ts#L129-L165","documentation":"Generic Error thrown by DifyKnowledgeServer.performListKnowledges when the GET /datasets request to the Dify API returns a non-2xx status. The error includes the status code and raw response body. It is caught by performListKnowledges' own try-catch and returned as an MCP isError response.","triggerScenarios":"GET {apiHost}/datasets with a Bearer token (DIFY_KEY) returns non-ok: 401/403 (invalid/expired key), 404 (wrong apiHost path), 5xx (Dify server error).","commonSituations":"DIFY_KEY is invalid, expired, or lacks knowledge-base read permissions; apiHost is wrong (missing /v1 suffix, trailing path mismatch); Dify instance is down or unreachable; network proxy returns an error.","solutions":["Verify DIFY_KEY is valid and has dataset read permissions (401/403).","Confirm the apiHost URL is correct and ends with the API base path (e.g., /v1).","Check that the Dify instance is running and reachable.","Inspect errorText for the Dify-specific error message."],"exampleFix":"// before\nif (!response.ok) {\n  const errorText = await response.text()\n  throw new Error(`API request failed, status code ${response.status}: ${errorText}`)\n}\n\n// after\nif (!response.ok) {\n  const errorText = await response.text()\n  if (response.status === 401) {\n    return errorResult('DIFY_KEY is invalid or expired. Please reconfigure the API key.')\n  }\n  throw new Error(`API request failed, status code ${response.status}: ${errorText}`)\n}","handlingStrategy":"try-catch","validationCode":"if (!difyKey) {\n  throw new Error('DIFY_KEY is not set — cannot list knowledge bases')\n}\nif (!apiHost || !/^https?:\\/\\//.test(apiHost)) {\n  throw new Error('Invalid Dify apiHost — must be a full URL')\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await performListKnowledges(difyKey, apiHost)\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e)\n  if (msg.includes('status code 401') || msg.includes('status code 403')) {\n    return { content: [{ type: 'text', text: 'DIFY_KEY is invalid or expired. Please reconfigure.' }], isError: true }\n  }\n  throw e\n}","preventionTips":["Verify DIFY_KEY is valid and has dataset read permissions.","Confirm the apiHost URL includes the correct API base path.","Test the key with a direct curl to /datasets before relying on the server."],"tags":["dify","network","http-error","api-key"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}