Skip to content

fix: replace process.exit() with custom Error throws (#18) - #20

Merged
nirvik34 merged 2 commits into
nirvik34:mainfrom
SAMI-CODEAI:Replace-process.exit()-with-custom-Error-throws-in-src/index.ts--sami.codeai
May 20, 2026
Merged

fix: replace process.exit() with custom Error throws (#18)#20
nirvik34 merged 2 commits into
nirvik34:mainfrom
SAMI-CODEAI:Replace-process.exit()-with-custom-Error-throws-in-src/index.ts--sami.codeai

Conversation

@SAMI-CODEAI

@SAMI-CODEAI SAMI-CODEAI commented May 17, 2026

Copy link
Copy Markdown
Contributor

Closes #18

Changes Made:

  • Replaced deep process.exit() calls in src/index.ts with explicit thrown Errors.
  • Wrapped the top-level execution inside bin/smartcommit.ts with a try/catch block.
  • Error messages are now safely logged to the console before process.exit(1) is exclusively called at the CLI entry point.
  • Verified test runner (vitest) stability; no longer abruptly terminates on failure states.

Summary by CodeRabbit

  • Bug Fixes

    • Consistent exit codes for success and failure scenarios.
    • Clearer, styled error messages for failures and cancellations.
    • Improved handling and messaging when not in a repository or when no changes are staged.
  • Tests

    • Added tests verifying error names and messages for validation and cancellation cases.

Review Change Stack

@vercel

vercel Bot commented May 17, 2026

Copy link
Copy Markdown

@SAMI-CODEAI is attempting to deploy a commit to the nirvik34's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

Warning

.coderabbit.yaml has a parsing error

The CodeRabbit configuration file in this repository has a parsing error and default settings were used instead. Please fix the error(s) in the configuration file. You can initialize chat with CodeRabbit to get help with the configuration file.

💥 Parsing errors (1)
Validation error: Invalid enum value. Expected 'chill' | 'assertive', received 'professional' at "reviews.profile"
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 976901cd-bcbd-40a8-a8e1-d5fc8e7279ec

📥 Commits

Reviewing files that changed from the base of the PR and between 80e90c4 and ba714ae.

📒 Files selected for processing (4)
  • bin/smartcommit.ts
  • src/index.test.ts
  • src/index.ts
  • src/utils/errors.ts

📝 Walkthrough

Walkthrough

The PR refactors error handling from direct process.exit() calls to exception-based control flow. The run() function in src/index.ts now throws errors for Git repository checks, staged changes validation, and commit cancellation. The CLI entry point in bin/smartcommit.ts catches these exceptions, logs them, and exits with appropriate status codes.

Changes

Error Handling Refactor

Layer / File(s) Summary
Add custom error classes
src/utils/errors.ts
Introduce GitBunError, ValidationError, and CancellationError with names and a default cancellation message.
Exception-based error control flow in run()
src/index.ts
run() imports the new errors and throws ValidationError for repository/staging checks and CancellationError when confirmation is rejected, replacing direct process.exit() usage.
CLI entry point error trapping and exit
bin/smartcommit.ts
program.action wraps run() in try/catch, uses chalk for styled error output, logs cancellation messages to stdout and other errors to stderr, and exits with codes 0 or 1 appropriately.
Tests: Errors suite
src/index.test.ts
Adds Errors tests asserting ValidationError and CancellationError name/message values (including default cancellation message).

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels: level: advanced, quality:clean, type:testing

🐰
I hopped through code with nimble paws,
Replaced abrupt exits with tidy laws.
Errors now bounce back to CLI’s gate,
Clean and testable — isn't that great?
🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: replacing process.exit() calls with custom error throws, which aligns perfectly with the primary objective of the changeset.
Linked Issues check ✅ Passed All coding requirements from issue #18 are met: src/index.ts now throws custom Error instances instead of calling process.exit(), and bin/smartcommit.ts wraps execution in try/catch to handle errors at the CLI entry point with proper exit codes.
Out of Scope Changes check ✅ Passed All changes are directly related to the PR objective: custom error classes (src/utils/errors.ts), error handling refactoring (src/index.ts), CLI-level error catching (bin/smartcommit.ts), and test coverage (src/index.test.ts) for the new error handling approach.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/index.ts`:
- Line 28: Remove ANSI formatting from thrown errors in src/index.ts (e.g., the
throw new Error(chalk.red("Not inside a Git repository.")) and similar
occurrences), and instead throw plain-text Error instances or a lightweight
custom class like UserFacingError(message, level) that carries a severity
property; then apply chalk coloring only in the top-level presentation layer
(bin/smartcommit.ts) when catching errors by inspecting the error type or
severity. Ensure all throw sites in src/index.ts use plain messages (or the
custom class) and update bin/smartcommit.ts's catch block to colorize based on
the error instance or its level.
- Line 98: The current throw new Error("Commit cancelled.") makes cancellation
indistinguishable from failures; change to a distinct control flow: create and
throw a custom CancellationError class (e.g., class CancellationError extends
Error) or have the run() function return a result object { cancelled: true }
instead of throwing; then update bin/smartcommit.ts to catch CancellationError
(or inspect the returned result) and exit with process.exit(0) for cancellations
while still treating other errors as process.exit(1). Locate the throw site (the
string "Commit cancelled.") and the run() function as the places to implement
the custom error/return and update the CLI entrypoint to handle it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7bab9769-36b3-4603-92a6-ea93ed56cd17

📥 Commits

Reviewing files that changed from the base of the PR and between f578f5c and 80e90c4.

📒 Files selected for processing (2)
  • bin/smartcommit.ts
  • src/index.ts

Comment thread src/index.ts Outdated
Comment thread src/index.ts Outdated
@nirvik34
nirvik34 requested a review from 2PieRadian May 17, 2026 14:27
@nirvik34

Copy link
Copy Markdown
Owner

@SAMI-CODEAI please resolve the coderabbit reviews

@nirvik34
nirvik34 merged commit d38c345 into nirvik34:main May 20, 2026
4 of 6 checks passed
@nirvik34
nirvik34 removed the request for review from 2PieRadian May 20, 2026 07:05
@nirvik34 nirvik34 added type: bug Something isn't working or smashes annoying bugs level:intermediate Decent knowledge required to work on quality:clean Bonus points under GSSOC for clean PR gssoc:approved Approved PR's under GSSOC mentor:nirvik34 labels May 20, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.9.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@SAMI-CODEAI
SAMI-CODEAI deleted the Replace-process.exit()-with-custom-Error-throws-in-src/index.ts--sami.codeai branch May 20, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved PR's under GSSOC level:intermediate Decent knowledge required to work on mentor:nirvik34 quality:clean Bonus points under GSSOC for clean PR released type: bug Something isn't working or smashes annoying bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REFACTOR] Replace process.exit() with custom Error throws in src/index.ts

2 participants