Skip to content

fix(cmd): exit non-zero when a command handler returns an error (#4255) - #4258

Open
NitinKumar004 wants to merge 2 commits into
gofr-dev:developmentfrom
NitinKumar004:fix/cmd-exit-code
Open

NitinKumar004 wants to merge 2 commits into
gofr-dev:developmentfrom
NitinKumar004:fix/cmd-exit-code

Conversation

@NitinKumar004

Copy link
Copy Markdown
Contributor

Description:

Fixes #4255.

CMD apps built with GoFr exited 0 even when the sub-command handler (or command
resolution) returned an error, so shells, pipelines, and CI could not detect the
failure.

Changes:

  • Responder now records whether it responded with a non-nil error (Errored()).
  • cmd.Run propagates that up to the caller.
  • runCMD exits with a non-zero status after telemetry is flushed and the logger
    is closed, so a failed command still reports its final metrics/traces before the
    process ends. os.Exit is wrapped in a small testable seam (osExit) so the exit
    path can be unit-tested without terminating the test binary — this also addresses
    the existing TODO in responder.go.

Behavior (verified end-to-end with a real CMD app):

  • successful command -> exit 0
  • handler returns an error -> exit 1
  • unknown command -> exit 1

Breaking Changes (if applicable):

None to the public API. The only behavioral change is the intended one: a CMD app now
exits non-zero when a command fails (previously it always exited 0).

Additional Information:

No new dependencies. cmd.Run's unexported signature changed to return a bool
(internal only).

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

…-dev#4255)

CMD apps exited 0 even when the sub-command handler (or command resolution)
returned an error, so shells and CI could not detect the failure.

Responder now records whether it responded with an error; cmd.Run propagates
that, and runCMD exits with a non-zero status after telemetry is flushed and the
logger is closed, so a failed command still reports its final metrics/traces.
os.Exit is wrapped in a testable seam.
The non-zero exit was terminating in-process tests that invoke Run (including
apps' own main() tests, e.g. examples/sample-cmd). Guard the exit with
testing.Testing() so real CLI binaries still exit non-zero while go test runs
are not killed. Extract the telemetry flush so its deferred cancel runs before
the exit, and cover cmd.Run's error/success/unknown-command return value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CMD apps always exit 0 even when the handler returns an error

1 participant