{"record":{"id":"4eec15f214909122","repo":"linshenkx/prompt-optimizer","slug":"failed-to-get-template-templateid-error-in","errorCode":null,"errorMessage":"Failed to get template \"${templateId}\": ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to get template \"(.+?)\": (.+?)","errorType":"exception","errorClass":"VariableExtractionExecutionError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-extraction/service.ts","lineNumber":137,"sourceCode":"   */\n  private async getExtractionTemplate(): Promise<Template> {\n    const templateId = 'variable-extraction';\n\n    try {\n      const template = await this.templateManager.getTemplate(templateId);\n      if (!template?.content) {\n        throw new VariableExtractionExecutionError(`Template \"${templateId}\" not found or empty.`);\n      }\n      return template;\n    } catch (error) {\n      if (error instanceof VariableExtractionError) {\n        throw error\n      }\n      // Preserve structured template errors if possible (code/params).\n      if (typeof (error as any)?.code === 'string') {\n        throw toErrorWithCode(error)\n      }\n      throw new VariableExtractionExecutionError(\n        `Failed to get template \"${templateId}\": ${error instanceof Error ? error.message : String(error)}`,\n      )\n    }\n  }\n\n  /**\n   * 构建模板上下文\n   */\n  private buildTemplateContext(request: VariableExtractionRequest): TemplateContext {\n    const context: TemplateContext = {\n      promptContent: request.promptContent,\n      existingVariableNames: request.existingVariableNames?.join(', ') || 'None',\n      hasExistingVariables: !!request.existingVariableNames?.length,\n    };\n\n    return context;\n  }\n","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-extraction/service.ts#L119-L155","documentation":"Wrapped error thrown when the underlying TemplateManager.getTemplate('variable-extraction') call itself throws an unexpected (non-structured) error — e.g. an I/O failure, a store connection error, or any exception without a machine-readable code. The original message is embedded so the root cause is not lost.","triggerScenarios":"TemplateManager.getTemplate throws for reasons other than 'not found': filesystem read errors, a database/HTTP template store being unreachable, deserialization bugs, or any thrown value that is not a VariableExtractionError and has no string code field. Structured errors with a code are re-thrown as-is; only uncoded ones become this wrapper.","commonSituations":"Template store backed by a DB/remote service that is down; permission errors reading the templates directory in a hardened container; corrupted template cache; version upgrade changed the template storage format causing loader exceptions.","solutions":["Read the embedded message — it is the root cause from the TemplateManager; fix that (permissions, connectivity, store config).","Verify the TemplateManager's backing store (files/DB/HTTP) is reachable and healthy.","Check file permissions on the templates directory if using file-based storage.","Clear/rebuild the template cache if it may be corrupted."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await extractionService.extract({ modelKey, content });\n} catch (e) {\n  if (e instanceof VariableExtractionExecutionError && e.message.startsWith('Failed to get template')) {\n    // inspect embedded root cause; likely template store I/O — retry after restoring the store\n  }\n  throw e;\n}","preventionTips":["Monitor template store (files/DB) health and add it to startup checks.","Wrap TemplateManager with retries or circuit breaking for transient I/O failures.","Log the full wrapped message — it carries the underlying error text for diagnosis."],"tags":["template","io","wrapped-error","template-manager"],"backgroundTag":"template-store-unavailable","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}