Skip to content

suggestCommand does not wrap getStagedDiff/getUnstagedDiff in try/catch, causing raw crashes on git errors #126

@404-Page-Found

Description

@404-Page-Found

Bug Description

suggestCommand does not wrap getStagedDiff() / getUnstagedDiff() in try/catch, causing unhandled crashes with raw stack traces on any git failure.

Steps to Reproduce

  1. Be inside a git repository
  2. Simulate a git error condition (e.g., run in a bare repository, or after the git binary is moved/removed from PATH after checkGitRepo() passes)
  3. Run commit-echo suggest

Expected Behavior

A user-friendly error message is displayed and the command exits gracefully.

Actual Behavior

The raw execSync error propagates up with an ugly stack trace.

Root Cause

src/commands/suggest.ts:108 — the diff retrieval is unprotected. This is inconsistent with other code paths:

  • checkGitRepo() at line 90 IS properly wrapped in try/catch (lines 89-94)
  • src/commands/batch.ts wraps its equivalent getGitDiff() in try/catch (lines 235-241)

Suggested Fix

Wrap the diff retrieval in try/catch:

let diffResult: DiffResult;
try {
  diffResult = getStagedDiff();
} catch (err) {
  outro(pc.red(`Failed to read staged diff: ${err instanceof Error ? err.message : String(err)}`));
  return;
}

Environment

  • commit-echo latest
  • Node.js (any version)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions