Skip to content

[Fix] : Replace unwrap()/expect() Usage with Robust Error Handling Across the Repository [Reopen: #42] #73

Description

@shaaravraghu

NEW CONTRIBUTORS FIRST PREFERENCE.

Description

The repository currently contains multiple usages of .unwrap() and .expect() in production code across several modules. While convenient during development, these calls can cause the application to panic unexpectedly when encountering runtime failures such as missing paths, poisoned locks, invalid options, or failed I/O operations.

This impacts the reliability and stability of ClipWallet, especially in user-facing or daemon-related workflows.

Examples

Some notable occurrences include:

  • src/config.rs

    home_dir().expect("No home dir")
  • src/daemon/plist.rs

    fs::create_dir_all(path.parent().unwrap())?;
  • Additional .unwrap() usages are present in:

    • src/main.rs
    • src/engine.rs
    • src/daemon/plist.rs
    • synchronization primitives (RwLock)
    • file/path operations
    • parsing and conversion logic

Why This Matters

  • Unexpected panics can terminate the application abruptly.
  • Panics reduce fault tolerance and worsen user experience.
  • Robust error propagation improves maintainability and debugging.
  • Following idiomatic Rust error handling practices improves overall code quality.

Suggested Improvements

  • Replace unnecessary .unwrap() and .expect() calls with:

    • ? operator for propagating errors
    • ok_or_else(...)
    • map_err(...)
    • graceful fallback handling where appropriate

Example Refactor

// Current
fs::create_dir_all(path.parent().unwrap())?;

// Suggested
let parent = path
    .parent()
    .ok_or_else(|| anyhow!("Invalid plist path"))?;

fs::create_dir_all(parent)?;

Scope

This issue aims to:

  • Audit .unwrap() and .expect() usage across the repository
  • Remove avoidable panics in production/runtime code
  • Improve error messages and propagation
  • Keep .unwrap() only where invariants are fully guaranteed or within tests

Acceptance Criteria

  • Reduce unsafe panic paths caused by .unwrap()/.expect()
  • Ensure meaningful errors are returned/logged instead of crashing
  • Pass existing tests and lint checks after refactor

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions