{"record":{"id":"0aa3e4fb2fec2136","repo":"Zie619/n8n-workflows","slug":"error-fetching-stats-str-e","errorCode":null,"errorMessage":"Error fetching stats: {str(e)}","messagePattern":"Error fetching stats: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"api_server.py","lineNumber":235,"sourceCode":"        \"\"\"\n        )\n    return FileResponse(str(index_file))\n\n\n@app.get(\"/health\")\nasync def health_check():\n    \"\"\"Health check endpoint.\"\"\"\n    return {\"status\": \"healthy\", \"message\": \"N8N Workflow API is running\"}\n\n\n@app.get(\"/api/stats\", response_model=StatsResponse)\nasync def get_stats():\n    \"\"\"Get workflow database statistics.\"\"\"\n    try:\n        stats = db.get_stats()\n        return StatsResponse(**stats)\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Error fetching stats: {str(e)}\")\n\n\n@app.get(\"/api/workflows\", response_model=SearchResponse)\nasync def search_workflows(\n    q: str = Query(\"\", description=\"Search query\"),\n    trigger: str = Query(\"all\", description=\"Filter by trigger type\"),\n    complexity: str = Query(\"all\", description=\"Filter by complexity\"),\n    active_only: bool = Query(False, description=\"Show only active workflows\"),\n    page: int = Query(1, ge=1, description=\"Page number\"),\n    per_page: int = Query(20, ge=1, le=100, description=\"Items per page\"),\n):\n    \"\"\"Search and filter workflows with pagination.\"\"\"\n    try:\n        offset = (page - 1) * per_page\n\n        workflows, total = db.search_workflows(\n            query=q,\n            trigger_filter=trigger,","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L217-L253","documentation":"A 500 response from GET /api/stats in api_server.py. The handler wraps db.get_stats() in a broad except and re-raises as HTTPException(500) with the original exception text. It means the workflow database (SQLite) could not be opened, is empty, or its schema does not match what WorkflowDB.get_stats() expects.","triggerScenarios":"Calling GET /api/stats when workflows.db does not exist (server started from a directory without an initialized database), when the DB file was created by an older schema version, or when get_stats() throws on empty/NULL aggregates after a partial index.","commonSituations":"Starting uvicorn from a different working directory so the relative DB path resolves elsewhere; cloning the repo and running the API before running the indexer; a crashed first indexing run leaving a schema-initialized but data-less DB.","solutions":["Run the indexer once to build the database before serving requests (python api_server.py or the indexing entrypoint used by this repo).","Start the server from the repository root so the relative SQLite/DB path and 'workflows' directory resolve correctly.","Delete the stale workflows.db and reindex if the schema was created by an incompatible version.","Reproduce the root cause by reading the server console: the underlying exception text is embedded in the detail string."],"exampleFix":"# before\nstats = db.get_stats()\n\n# after (guard with a friendly message when DB is unindexed)\ntry:\n    stats = db.get_stats()\nexcept Exception:\n    raise HTTPException(status_code=503, detail=\"Database not initialized. Run the workflow indexer first.\")","handlingStrategy":"try-catch","validationCode":"import urllib.request, json\n\ndef stats_ok(base):\n    try:\n        with urllib.request.urlopen(base + '/api/stats', timeout=5) as r:\n            return r.status == 200 and 'total_workflows' in json.load(r)\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    stats = client.get('/api/stats').json()\nexcept (ConnectionError, Timeout):\n    print('API unreachable')\nexcept KeyError:\n    print('Unexpected stats payload — reindex the database')","preventionTips":["Run the indexer as part of deployment before exposing the API.","Health-check /api/stats in your process manager startup probe.","Start the server from a fixed working directory so relative DB paths resolve."],"tags":["fastapi","database","initialization","http-500"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}