{"record":{"id":"4743c22668b509b0","repo":"linshenkx/prompt-optimizer","slug":"error-instanceof-error-error-message-string-er-4743c2","errorCode":null,"errorMessage":"error instanceof Error ? error.message : String(error)","messagePattern":"error instanceof Error \\? error\\.message : String\\(error\\)","errorType":"exception","errorClass":"VariableValueGenerationExecutionError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":68,"sourceCode":"\n    // 4. 构建模板上下文\n    const context = this.buildTemplateContext(request);\n\n    // 5. 使用 TemplateProcessor 渲染模板\n    const messages = TemplateProcessor.processTemplate(template, context);\n\n    // 6. 调用 LLM 发送请求\n    try {\n      const result = await this.llmService.sendMessage(messages, request.generationModelKey);\n\n      // 7. 解析 LLM 返回的 JSON 结果（传递请求的变量列表用于对齐校验）\n      return this.parseGenerationResult(result, request.variables);\n    } catch (error) {\n      // 🔧 修复：保留原始错误类型，不要过度包装\n      if (error instanceof VariableValueGenerationError) {\n        throw error;\n      }\n      throw new VariableValueGenerationExecutionError(error instanceof Error ? error.message : String(error))\n    }\n  }\n\n  /**\n   * 验证请求参数\n   */\n  private validateRequest(request: VariableValueGenerationRequest): void {\n    if (!request.promptContent?.trim()) {\n      throw new VariableValueGenerationValidationError('Prompt content must not be empty.');\n    }\n\n    if (!request.generationModelKey?.trim()) {\n      throw new VariableValueGenerationValidationError('Generation model key must not be empty.');\n    }\n\n    if (!request.variables || request.variables.length === 0) {\n      throw new VariableValueGenerationValidationError('Variables list must not be empty.');\n    }","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L50-L86","documentation":"This is the generic catch-all in VariableValueGenerationService.generate: any unexpected error that is not already a VariableValueGeneration subtype gets wrapped in VariableValueGenerationExecutionError with the original message preserved. It typically surfaces network failures, provider auth errors, or model invocation problems that happened while calling the generation model.","triggerScenarios":"Calling generate() when the underlying model call throws — e.g. the modelManager invocation fails (connection refused, 401 API key, 429 rate limit, timeout), or template rendering raises an unexpected exception.","commonSituations":"Missing/expired API key for the configured model; model endpoint unreachable (proxy/firewall/DNS); rate limits from the provider; the model key exists but its provider is misconfigured; disk/permission errors reading templates.","solutions":["Read the wrapped message — it preserves the original error (auth, network, rate-limit text)","Verify the model key passed as generationModelKey is configured and reachable (test with a minimal direct model call)","Check API keys and environment variables for the model provider","If it is a transient network/rate-limit error, retry generate() with backoff"],"exampleFix":"// before\nconst res = await genService.generate(req);\n\n// after\ntry {\n  const res = await genService.generate(req);\n} catch (e) {\n  if (e instanceof VariableValueGenerationExecutionError) {\n    console.error('underlying failure:', e.message); // inspect original cause\n  }\n  throw e;\n}","handlingStrategy":"retry","validationCode":"const model = await modelManager.getModel(req.generationModelKey);\nif (!model) throw new Error('model unavailable before generate()');","typeGuard":null,"tryCatchPattern":"try { await gen.generate(req); } catch (e) { if (e instanceof VariableValueGenerationExecutionError && !(e instanceof VariableValueGenerationParseError) && !(e instanceof VariableValueGenerationValidationError)) { /* inspect e.message for provider/network cause; retry with backoff if transient */ } throw e; }","preventionTips":["Health-check the model provider (key, connectivity) before launching generation batches","Wrap generate() in a bounded retry with exponential backoff for transient provider errors","Never swallow the wrapped message — it carries the root cause"],"tags":["model-invocation","network","wrapper-error","variable-value-generation"],"backgroundTag":"llm-provider-request-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}