{"record":{"id":"905497e18f630ff5","repo":"linshenkx/prompt-optimizer","slug":"failed-to-get-template-templateid-error-in-905497","errorCode":null,"errorMessage":"Failed to get template \"${templateId}\": ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to get template \"(.+?)\": (.+?)","errorType":"exception","errorClass":"VariableValueGenerationExecutionError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/variable-value-generation/service.ts","lineNumber":126,"sourceCode":"   * 获取变量值生成模板\n   */\n  private async getGenerationTemplate(): Promise<Template> {\n    const templateId = 'variable-value-generation';\n\n    try {\n      const template = await this.templateManager.getTemplate(templateId);\n      if (!template?.content) {\n        throw new VariableValueGenerationExecutionError(`Template \"${templateId}\" not found or empty.`);\n      }\n      return template;\n    } catch (error) {\n      if (error instanceof VariableValueGenerationError) {\n        throw error\n      }\n      if (typeof (error as any)?.code === 'string') {\n        throw toErrorWithCode(error)\n      }\n      throw new VariableValueGenerationExecutionError(\n        `Failed to get template \"${templateId}\": ${error instanceof Error ? error.message : String(error)}`,\n      )\n    }\n  }\n\n  /**\n   * 构建模板上下文\n   */\n  private buildTemplateContext(request: VariableValueGenerationRequest): TemplateContext {\n    const formatVariables = (variables: VariableToGenerate[]): string => variables\n      .map((v, idx) => {\n        const parts = [`${idx + 1}. ${v.name}`];\n        if (v.description?.trim()) parts.push(`(description: ${v.description.trim()})`);\n        if (v.defaultValue?.trim()) parts.push(`(default value: ${v.defaultValue.trim()})`);\n        if (v.currentValue) parts.push(`(current value: ${v.currentValue})`);\n        if (v.source) parts.push(`[${v.source}]`);\n        return parts.join(' ');\n      })","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/variable-value-generation/service.ts#L108-L144","documentation":"While fetching the 'variable-value-generation' template, the template manager threw an unexpected error (with no string code property and not already a VariableValueGenerationError). The service wraps it into VariableValueGenerationExecutionError with the original message appended. Typical causes are I/O or storage-layer failures rather than a missing template.","triggerScenarios":"templateManager.getTemplate('variable-value-generation') rejects due to a database connection error, file permission denied on the templates directory, corrupted template store, or an unserialized/corrupt template record.","commonSituations":"Templates stored in SQLite whose file is locked or corrupted; read-only filesystem in a container; template storage path env var pointing to a nonexistent/unwritable directory; a partially applied migration leaving corrupt rows.","solutions":["Read the appended original message in the error string — it identifies the storage failure","Check filesystem permissions / DB connectivity for the template store","Point template storage at a valid writable location (env/config) and restart","Restore the template store from backup or reseed defaults if corrupted","Note: errors with a string code property pass through unchanged — check that subtype for coded storage errors"],"exampleFix":"// before\nawait gen.generate(req);\n\n// after\ntry {\n  await gen.generate(req);\n} catch (e) {\n  if (e instanceof VariableValueGenerationExecutionError && e.message.includes('Failed to get template')) {\n    // storage-level issue: verify template store health before retrying\n    await assertTemplateStoreHealthy(templateManager);\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (e) { if (e instanceof VariableValueGenerationExecutionError && e.message.startsWith('Failed to get template')) { const cause = e.message.split(':').slice(1).join(':').trim(); /* route to storage diagnostics based on cause */ } throw e; }","preventionTips":["Monitor template storage health (DB/file) alongside the app","Distinguish coded storage errors (which pass through with .code) from wrapped ones when logging"],"tags":["template-config","storage-io","wrapper-error","variable-value-generation"],"backgroundTag":"template-store-unavailable","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}