{"record":{"id":"a42663e0e8e03fa0","repo":"linshenkx/prompt-optimizer","slug":"generation-failed","errorCode":"GENERATION_FAILED","errorMessage":"DashScope API error: ${data.code}","messagePattern":"DashScope API error: (.+?)","errorType":"error_code","errorClass":"ImageError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/image/adapters/dashscope.ts","lineNumber":315,"sourceCode":"\n    if (!response.ok) {\n      let errorMessage = `DashScope API error: ${response.status} ${response.statusText}`\n      try {\n        const errorData = await response.json()\n        if (errorData.message) {\n          errorMessage = errorData.message\n        }\n      } catch {\n        // 忽略 JSON 解析错误\n      }\n      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, errorMessage)\n    }\n\n    const data = await response.json()\n\n    // 检查是否有错误\n    if (data.code) {\n      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, data.message || `DashScope API error: ${data.code}`)\n    }\n\n    // 解析 Qwen-Image 响应\n    const choices = data.output?.choices || []\n    if (choices.length === 0) {\n      throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)\n    }\n\n    // 只取第一张图像\n    const choice = choices[0]\n    const content = choice.message?.content || []\n    const imageContent = content.find((c: any) => c.image)\n    if (!imageContent) {\n      throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)\n    }\n\n    return {\n      images: [{","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/image/adapters/dashscope.ts#L297-L333","documentation":"Thrown by the DashScope adapter's generateWithQwenImage when the Qwen-Image generation API returns a JSON body containing a top-level code field, which DashScope uses to signal API-level errors (auth failure, quota, invalid parameters, content policy). The message prefers the API's own message and falls back to 'DashScope API error: <code>'.","triggerScenarios":"Calling doGenerate on the DashScope image adapter with an invalid/expired API key (code like InvalidApiKey), exhausted quota, unknown model id, or a policy-flagged prompt; DashScope returning 200 with an error body.","commonSituations":"Wrong DASHSCOPE_API_KEY or expired key; free-tier quota exhausted; model name typo (e.g. misspelled qwen-image variant); prompts triggering content moderation; DashScope regional endpoint mismatch.","solutions":["Inspect the error message / data.code to identify the exact DashScope error (auth vs quota vs parameter).","Verify the API key is set, valid, and has image-generation entitlements; rotate if expired.","Check quota/billing on the DashScope console and the exact model id spelling.","If content-policy flagged, rephrase the prompt and retry."],"exampleFix":"// before\nawait adapter.doGenerate({ model: 'qwen-image', prompt: '...' }) // bad key -> throws\n\n// after\nprocess.env.DASHSCOPE_API_KEY = await vault.get('dashscope')\nawait adapter.doGenerate({ model: 'qwen-image', prompt: '...' })","handlingStrategy":"retry","validationCode":null,"typeGuard":"const isDashScopeImageError = (e: unknown): e is ImageError =>\n  e instanceof ImageError && e.code === IMAGE_ERROR_CODES.GENERATION_FAILED","tryCatchPattern":"try {\n  await adapter.doGenerate(req)\n} catch (e) {\n  if (e instanceof ImageError && e.code === IMAGE_ERROR_CODES.GENERATION_FAILED) {\n    if (/quota|Throttling/i.test(e.message)) await sleep(backoff).then(retry) // retryable\n    else throw e // auth/param errors need user action\n  }\n}","preventionTips":["Keep DASHSCOPE_API_KEY fresh and validate it at startup","Check quota before batch generation","Log data.code to classify failures"],"tags":["dashscope","api-error","image-generation","network"],"backgroundTag":"upstream-api-error","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}