fix(cmd): replace panics with returned errors - #72
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses Issue #65 by updating CLI commands to return errors (via Cobra’s RunE) instead of panicking, so failures flow through cmd/root.go’s centralized Execute() error printing.
Changes:
- Converted
save,history, andrestorecommands fromRuntoRunEand replacedpanic(err)withreturn err. - Adjusted
historyJSON output path to returnnilinstead of exiting early. - Updated command tests to invoke
RunEinstead ofRun.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| cmd/save.go | Switches save to RunE and returns snapshot creation errors instead of panicking. |
| cmd/restore.go | Switches restore to RunE and returns snapshot restore errors instead of panicking. |
| cmd/history.go | Switches history to RunE and returns DB/JSON errors instead of panicking. |
| cmd/commands_test.go | Updates tests to call RunE entrypoints instead of Run. |
Comments suppressed due to low confidence (8)
cmd/save.go:43
db.InitDB()returns a *sql.DB, but this command never closes it and also ignores the error fromExec. That can leak DB resources and can print "Snapshot saved" even if the insert failed. Close the DB and return theExecerror so the CLI's clean error path can report it.
database := db.InitDB()
database.Exec(
"INSERT INTO snapshots(id, message, path) VALUES (?, ?, ?)",
id,
saveMessage,
cmd/commands_test.go:122
- This test discards the error from
RunE, so it can pass even if the command fails. Assert thatRunEreturns nil.
_ = saveCmd.RunE(saveCmd, []string{})
cmd/commands_test.go:133
- This test captures stdout but discards the error from
RunE. Capture the error, restore stdout, then fail the test if the command returned an error (so failures don't get hidden and stdout always gets restored).
This issue also appears on line 166 of the same file.
_ = historyCmd.RunE(historyCmd, []string{})
w.Close()
os.Stdout = oldStdout
cmd/commands_test.go:150
- This test discards the error from
RunE, so it can pass even if the command fails. Assert thatRunEreturns nil.
_ = saveCmd.RunE(saveCmd, []string{})
cmd/commands_test.go:170
- This test captures stdout but discards the error from
RunE. Capture the error, restore stdout, then fail the test if the command returned an error (so failures don't get hidden and stdout always gets restored).
_ = historyCmd.RunE(historyCmd, []string{})
w.Close()
os.Stdout = oldStdout
cmd/commands_test.go:197
- This test discards the error from
RunE, so it can pass even if the command fails. Assert thatRunEreturns nil.
_ = saveCmd.RunE(saveCmd, []string{})
cmd/commands_test.go:214
- This test discards the error from
RunE, so it can pass even if the command fails. Assert thatRunEreturns nil.
_ = restoreCmd.RunE(restoreCmd, []string{id})
cmd/commands_test.go:262
- This test discards the error from
RunE, so it can pass even if the command fails. Assert thatRunEreturns nil.
_ = saveCmd.RunE(saveCmd, []string{})
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
18
to
22
| database := db.InitDB() | ||
| var path string | ||
| database.QueryRow("SELECT path FROM snapshots WHERE id=?", id).Scan(&path) | ||
| err := snapshot.RestoreSnapshot(path) | ||
| if err != nil { |
Comment on lines
36
to
40
| if err := rows.Scan(&entry.ID, &entry.CreatedAt); err != nil { | ||
| panic(err) | ||
| return err | ||
| } | ||
| entries = append(entries, entry) | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed #65