-
Notifications
You must be signed in to change notification settings - Fork 84
cli: add error handling with panic recovery inside main() #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,8 @@ package main | |||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "embed" | ||||||||||||||||||
| "fmt" | ||||||||||||||||||
| "os" | ||||||||||||||||||
|
|
||||||||||||||||||
| "github.com/volcano-sh/kthena/cli/kthena/cmd" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
@@ -30,5 +32,13 @@ var templatesFS embed.FS | |||||||||||||||||
|
|
||||||||||||||||||
| func main() { | ||||||||||||||||||
| cmd.InitTemplates(templatesFS) | ||||||||||||||||||
|
|
||||||||||||||||||
| defer func() { | ||||||||||||||||||
| if r := recover(); r != nil { | ||||||||||||||||||
| fmt.Println("Recovered:", r) | ||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+36
to
+40
|
||||||||||||||||||
| }() | ||||||||||||||||||
| cmd.Execute() | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To make this more robust, consider changing |
||||||||||||||||||
| os.Exit(0) | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call to
|
||||||||||||||||||
| os.Exit(0) |
Copilot
AI
Mar 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this deferred recovery won’t run for the common non-panic error path of cmd.Execute(): Execute() currently calls os.Exit(1) when rootCmd.Execute() returns an error, and os.Exit bypasses defers in main. If the goal is centralized error handling (and to preserve defers), consider changing cmd.Execute() to return an error instead of exiting, and handle the exit code in main.
| cmd.Execute() | |
| os.Exit(0) | |
| if err := cmd.Execute(); err != nil { | |
| fmt.Fprintf(os.Stderr, "Error : %v\n", err) | |
| os.Exit(1) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recovered panic message formatting looks off ("Error :" has an extra space and differs from other stderr messages like "warning:"). Also, printing only %v loses useful context for debugging panics; consider including a stack trace (e.g., runtime/debug.Stack) and using a consistent prefix like "panic:" or "error:".