{"record":{"id":"08fab12d2b582c7a","repo":"ZhuLinsen/daily_stock_analysis","slug":"not-found","errorCode":"not_found","errorMessage":"Alert rule not found: {rule_id}","messagePattern":"Alert rule not found: (.+?)","errorType":"exception","errorClass":"AlertNotFoundError","httpStatus":404,"severity":"warning","filePath":"api/v1/endpoints/alerts.py","lineNumber":153,"sourceCode":"    except UnsupportedAlertTypeError as exc:\n        raise _bad_request(exc, error=exc.error_code)\n    except AlertServiceError as exc:\n        raise _bad_request(exc, error=exc.error_code)\n    except Exception as exc:\n        raise _internal_error(\"Update alert rule failed\", exc)\n\n\n@router.delete(\n    \"/rules/{rule_id}\",\n    response_model=AlertDeleteResponse,\n    responses={404: {\"model\": ErrorResponse}, 500: {\"model\": ErrorResponse}},\n    summary=\"Delete alert rule\",\n)\ndef delete_rule(rule_id: int) -> AlertDeleteResponse:\n    service = AlertService()\n    try:\n        if not service.delete_rule(rule_id):\n            raise AlertNotFoundError(f\"Alert rule not found: {rule_id}\")\n        return AlertDeleteResponse(deleted=1)\n    except AlertNotFoundError as exc:\n        raise _not_found(exc)\n    except Exception as exc:\n        raise _internal_error(\"Delete alert rule failed\", exc)\n\n\n@router.post(\n    \"/rules/{rule_id}/enable\",\n    response_model=AlertRuleItem,\n    responses={404: {\"model\": ErrorResponse}, 500: {\"model\": ErrorResponse}},\n    summary=\"Enable alert rule\",\n)\ndef enable_rule(rule_id: int) -> AlertRuleItem:\n    service = AlertService()\n    try:\n        return AlertRuleItem(**service.enable_rule(rule_id, True))\n    except AlertNotFoundError as exc:","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/api/v1/endpoints/alerts.py#L135-L171","documentation":"DELETE /alerts/rules/{rule_id} returns HTTP 404 code=not_found when AlertService.delete_rule(rule_id) reports the rule as not deleted — i.e. no row with that integer id exists (alerts.py:145-153). The endpoint wraps AlertNotFoundError via _not_found; any other exception becomes a 500 with 'Delete alert rule failed'.","triggerScenarios":"Deleting a rule that was already removed (double delete, two admin sessions); passing an id from a stale list page; passing a non-existent or malformed id (non-integer ids fail earlier at path parsing with FastAPI's 422); rule id drift after a database reset/migration.","commonSituations":"Web UI list not refreshed after another client deleted the rule; SQLite file swapped or reset between listing and deleting; automation scripts hard-coding rule ids across environments; concurrent delete requests racing so the second one 404s.","solutions":["Confirm the rule still exists via GET /alerts/rules before deleting, or simply handle 404 as 'already gone' in idempotent delete flows","Refresh the rule list in the UI and retry the delete against a current id","If the id should exist, verify the API server points at the same database file/DSN the rules were created in","Check server logs for the 500 branch ('Delete alert rule failed') in case the not-found result is actually masking a service-level failure"],"exampleFix":"# before\nresp = requests.delete(f\"{base}/alerts/rules/{rule_id}\")\nresp.raise_for_status()\n\n# after\nresp = requests.delete(f\"{base}/alerts/rules/{rule_id}\")\nif resp.status_code == 404:\n    logger.info(\"rule %s already deleted\", rule_id)  # idempotent success\nelse:\n    resp.raise_for_status()","handlingStrategy":"try-catch","validationCode":"# Python client: pre-check existence for non-idempotent flows\nimport requests\nif requests.get(f\"{BASE}/api/v1/alerts/rules/{rule_id}\").status_code == 200:\n    resp = requests.delete(f\"{BASE}/api/v1/alerts/rules/{rule_id}\")\nelse:\n    log(f\"rule {rule_id} already gone\")","typeGuard":null,"tryCatchPattern":"try:\n    delete_rule(rule_id)\nexcept HTTPError as e:\n    if e.response.status_code == 404:\n        return  # already deleted — idempotent success\n    raise","preventionTips":["Refresh the rule list before rendering delete actions","Treat delete as idempotent in automation scripts (404 == done)","Ensure all admin clients point at the same alerts DB"],"tags":["alerts","http-404","delete","idempotency"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}