{"record":{"id":"0404cfc144e1f598","repo":"ZhuLinsen/daily_stock_analysis","slug":"intelligence-source-template-not-found-template","errorCode":null,"errorMessage":"Intelligence source template not found: {template_id}","messagePattern":"Intelligence source template not found: (.+?)","errorType":"exception","errorClass":"IntelligenceServiceError","httpStatus":404,"severity":"error","filePath":"src/services/intelligence_service.py","lineNumber":182,"sourceCode":"    def list_source_templates(self, **filters: Any) -> Dict[str, Any]:\n        market = str(filters.get(\"market\") or \"\").strip().lower()\n        source_type = str(filters.get(\"source_type\") or \"\").strip().lower()\n        templates = []\n        for template in self._builtin_source_templates():\n            if market and template[\"market\"] != market:\n                continue\n            if source_type and template[\"source_type\"] != source_type:\n                continue\n            templates.append(dict(template))\n        return {\"items\": templates, \"total\": len(templates)}\n\n    def create_source_from_template(self, template_id: str, overrides: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:\n        selected = next(\n            (dict(template) for template in self._builtin_source_templates() if template[\"template_id\"] == template_id),\n            None,\n        )\n        if selected is None:\n            raise IntelligenceServiceError(f\"Intelligence source template not found: {template_id}\")\n        payload = {key: value for key, value in selected.items() if key != \"template_id\"}\n        payload.update({key: value for key, value in (overrides or {}).items() if value is not None})\n        return self.create_source(payload)\n\n    def create_default_sources(self, overrides: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:\n        request_fields = dict(overrides or {})\n        request_fields.setdefault(\"enabled\", False)\n        created_count = 0\n        items = []\n        for template in self._builtin_source_templates():\n            payload = {key: value for key, value in template.items() if key != \"template_id\"}\n            payload.update({key: value for key, value in request_fields.items() if value is not None})\n            existing = self.repo.get_source_by_name(str(payload[\"name\"]))\n            if existing is not None:\n                items.append({\"created\": False, \"source\": self._source_to_dict(existing)})\n                continue\n            source = self.create_source(payload)\n            created_count += 1","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/intelligence_service.py#L164-L200","documentation":"create_source_from_template looks up template_id only in _builtin_source_templates(); unknown ids raise this IntelligenceServiceError. Templates are a fixed in-code catalog, not user data — there is no DB fetch involved.","triggerScenarios":"Calling with a stale/typo'd template_id after built-in template ids were renamed or removed in an upgrade; client caching an old template list.","commonSituations":"Frontend dropdown built from an older API version; API consumer hardcoding a template id that a release removed; trailing whitespace/case mismatch in the id.","solutions":["Re-fetch the current template list via list_source_templates and use an id from the response","Check spelling/case of the template_id being sent","If a needed template vanished, create the source directly via create_source with equivalent fields"],"exampleFix":"# before\nsvc.create_source_from_template('cls-old-id')  # removed -> error\n# after\ntemplates = svc.list_source_templates()['items']\nsvc.create_source_from_template(templates[0]['template_id'])","handlingStrategy":"validation","validationCode":"valid_ids = {t['template_id'] for t in svc.list_source_templates()['items']}\nif template_id not in valid_ids:\n    refresh_template_list()  # ids may have changed across versions","typeGuard":"def is_known_template(svc, tid: str) -> bool:\n    return any(t['template_id'] == tid\n               for t in svc.list_source_templates()['items'])","tryCatchPattern":"try:\n    svc.create_source_from_template(tid, overrides)\nexcept IntelligenceServiceError as e:\n    if 'template not found' in str(e):\n        templates = svc.list_source_templates()['items']\n        tid = prompt_user_to_pick(templates)['template_id']\n        svc.create_source_from_template(tid, overrides)\n    else:\n        raise","preventionTips":["Always send template ids taken from a live list_source_templates response","Never hardcode template ids in clients — they are a server-owned catalog","Treat unknown-template as a refresh-and-retry UX flow, not a crash"],"tags":["intelligence","template","not-found"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}