Skip to content

Bug: Bare except clauses can hide critical exceptions (7 instances) #70

@Serhan-Asad

Description

@Serhan-Asad

Summary

There are 7 instances of bare except: clauses throughout the codebase that catch ALL exceptions including KeyboardInterrupt and SystemExit. This is a Python anti-pattern that prevents proper program termination and can hide critical errors.

Impact

Severity: Medium
Type: Code Quality / Reliability

Bare except: clauses can:

  • Prevent users from gracefully terminating the program with Ctrl+C (KeyboardInterrupt)
  • Hide system exit signals (SystemExit)
  • Mask unexpected errors that should propagate
  • Make debugging significantly harder

Affected Files

  1. pdd/fix_errors_from_unit_tests.py:88, 95

    • Silently ignores errors when cleaning up temporary files
    try:
        os.unlink(tmp_path)
    except:  # BAD: Catches KeyboardInterrupt, SystemExit, etc.
        pass
  2. pdd/construct_paths.py:266

    • Hides git repository detection failures
    try:
        import git
        repo = git.Repo(file_path, search_parent_directories=True)
        repo_root = repo.working_tree_dir
    except:  # BAD: Should catch specific git exceptions
        repo_root = os.getcwd()
  3. pdd/update_main.py:65

    • Swallows git errors without logging
    try:
        import git
        repo = git.Repo(code_dir, search_parent_directories=True)
        repo_root = repo.working_tree_dir
    except:  # BAD: No logging of actual error
        pass
  4. pdd/unfinished_prompt.py:108

    • Hides print/rprint errors
    try:
        rprint(f"Input text: {prompt_text}")
    except:  # BAD: If rprint fails, should know why
        print(f"Input text: {prompt_text}")
  5. pdd/setup_tool.py:293

    • Swallows shell detection errors
    try:
        shell_path = os.getenv('SHELL', '/bin/bash')
        shell_name = os.path.basename(shell_path)
        return shell_name
    except:  # BAD: os.path.basename shouldn't fail on valid input
        return 'bash'
  6. pdd/agentic_common.py:549

    • Hides JSON parsing errors
    try:
        data = json.loads(lines[-1])
    except:  # BAD: Should be json.JSONDecodeError
        pass

Recommended Fix

Replace all bare except: clauses with specific exception types:

# Instead of:
try:
    os.unlink(tmp_path)
except:
    pass

# Use:
try:
    os.unlink(tmp_path)
except (OSError, FileNotFoundError):
    pass  # File already deleted or doesn't exist

# Or for cleanup:
try:
    os.unlink(tmp_path)
except Exception as e:
    # Still allows KeyboardInterrupt and SystemExit to propagate
    pass

References

Steps to Reproduce

  1. Run any PDD command that triggers these code paths
  2. Press Ctrl+C during execution
  3. Observe that some cleanup handlers may prevent graceful termination

Additional Context

While most of these are in cleanup/fallback code where the impact is lower, it's still best practice to catch specific exceptions. This makes the code more maintainable and helps identify unexpected error conditions during development.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions