code-review-graph icon indicating copy to clipboard operation
code-review-graph copied to clipboard

Replace bare `except Exception` with specific exception handling and logging

Open VaibhavDangaich opened this issue 7 months ago • 2 comments

Summary

Multiple locations in the codebase use bare except Exception: (or except Exception without logging) that silently swallow errors, returning empty defaults. This makes debugging extremely difficult
because real bugs, misconfigured environments, and corrupted state produce no visible signal — they just silently degrade.

Affected Locations

File Line(s) Function Current Behavior
graph.py 758 get_node_community_map() Returns {} on any error
graph.py 833 get_communities_list() Returns [] on any error
parser.py 304 _get_parser() Returns None silently
wiki.py 121, 161 _generate_community_page() Replaces data with "not available"
visualization.py 145, 152 export helpers Silent fallback
tsconfig_resolver.py 55 resolver init Silent skip
migrations.py 241 migration logic Silent skip
registry.py 195, 212 connection pool Silent skip
cli.py 43 _get_version() Returns "dev" on any error
eval/benchmarks/search_quality.py 21 search eval Silent skip
tools/context.py 88, 98, 108 context loading Silent skip (has nosec comments)

Note: Some except Exception as e: usages (e.g., in incremental.py, search.py, tools/*.py) already log the error or return it in structured output — those are fine and not part of this issue.

Proposed Fix

For each location:

  1. Catch specific exceptions where possible (e.g., sqlite3.OperationalError for DB queries, KeyError/LookupError for tree-sitter parser loading)
  2. Add logger.debug() or logger.warning() with the exception so errors are visible when debugging
  3. Keep the graceful fallback behavior (returning empty defaults is correct for optional features like communities)

Example

# Before (graph.py:758)
try:                                                                                                                                                                                                         
    rows = self._conn.execute(
        "SELECT qualified_name, community_id FROM nodes"                                                                                                                                                     
    ).fetchall()                                                                                                                                                                                           
    return {r["qualified_name"]: r["community_id"] for r in rows}
except Exception:
    return {}
                                                                                                                                                                                                             
# After
try:                                                                                                                                                                                                         
    rows = self._conn.execute(                                                                                                                                                                             
        "SELECT qualified_name, community_id FROM nodes"
    ).fetchall()
    return {r["qualified_name"]: r["community_id"] for r in rows}
except sqlite3.OperationalError as e:
    logger.debug("Community map unavailable: %s", e)                                                                                                                                                         
    return {}
                                                                                                                                                                                                             
Priority Tiers                                                                                                                                                                                             

- High: graph.py, parser.py — core functionality, errors here mask real bugs
- Medium: wiki.py, registry.py, migrations.py — user-facing features
- Low: cli.py:43 (version fallback), tools/context.py (already has nosec justification)                                                                                                                      
 
Checklist                                                                                                                                                                                                    
                                                                                                                                                                                                           
- Replace bare except Exception: with specific exception types                                                                                                                                               
- Add logger.debug() or logger.warning() to each handler
- Ensure existing tests still pass (uv run pytest tests/ --tb=short -q)                                                                                                                                      
- Add test for at least one case (e.g., verify get_node_community_map() logs on DB error)                                                                                                                  
                                                                                                                                                                                                             
Environment
                                                                                                                                                                                                             
- Python: 3.10+                                                                                                                                                                                            
- OS: Any
- Version: 2.2.2

---

You can file this at: **https://github.com/tirth8205/code-review-graph/issues/new**                                                                                                                          
 
If you'd like, I can also help you implement the fix so you can submit a PR alongside the issue. The other two issues (legacy DB migration crash, unbounded parse workers) are also good follow-up           
contributions.                   

VaibhavDangaich avatar Apr 09 '26 19:04 VaibhavDangaich

This is a well-documented quality improvement. The locations listed (graph.py, parser.py, wiki.py, visualization.py, tsconfig_resolver.py, migrations.py, registry.py, cli.py, eval/benchmarks/search_quality.py, tools/context.py) all have bare except Exception blocks that swallow errors silently.

The proposed fix is correct: catch specific exception types (sqlite3.OperationalError, LookupError, KeyError, OSError) and add logger.debug()/warning() calls so errors are visible when debugging, while keeping graceful fallback behavior.

This is a valid code quality bug. A PR implementing these changes would be welcomed — it improves debuggability without changing user-facing behavior.

tirth8205 avatar Apr 11 '26 08:04 tirth8205

@tirth8205 can u merge the pull request I raised related to this issue?!

VaibhavDangaich avatar Apr 11 '26 21:04 VaibhavDangaich