Skip to content

fix(cmd): replace panics with returned errors - #72

Merged
kavix merged 3 commits into
kavix:mainfrom
vomar3:main
Jul 27, 2026
Merged

fix(cmd): replace panics with returned errors#72
kavix merged 3 commits into
kavix:mainfrom
vomar3:main

Conversation

@vomar3

@vomar3 vomar3 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Fixed #65

Copilot AI 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.

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, and restore commands from Run to RunE and replaced panic(err) with return err.
  • Adjusted history JSON output path to return nil instead of exiting early.
  • Updated command tests to invoke RunE instead of Run.

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 from Exec. That can leak DB resources and can print "Snapshot saved" even if the insert failed. Close the DB and return the Exec error 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 that RunE returns 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 that RunE returns 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 that RunE returns 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 that RunE returns 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 that RunE returns nil.
	_ = saveCmd.RunE(saveCmd, []string{})

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/restore.go
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 thread cmd/history.go
Comment on lines 36 to 40
if err := rows.Scan(&entry.ID, &entry.CreatedAt); err != nil {
panic(err)
return err
}
entries = append(entries, entry)
}
Comment thread cmd/commands_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@kavix
kavix merged commit 9e603d2 into kavix:main Jul 27, 2026
2 checks passed
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.

Replace panic(err) with returned errors for consistent CLI error output

3 participants