Skip to content
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

Fix panic handling and add a new exit code and a test for panics #2997

Merged
merged 1 commit into from Mar 31, 2023

Conversation

na--
Copy link
Member

@na-- na-- commented Mar 31, 2023

In #2833 we started calling os.Exit() in a defer statement, so we inadvertently circumvented the regular handling of panics that bubble up to the top by the Go runtime.

I could have removed the os.Exit() call from the defer, but I think this is better. Handling panics ourselves in this way is explicit, simpler to understand, and it allows us to handle them exactly how we want - including assigning them their own unique exit code, and test the whole thing with an e2e test.

@na-- na-- requested a review from codebien March 31, 2023 09:01
@na-- na-- added this to the v0.44.0 milestone Mar 31, 2023
imiric
imiric previously approved these changes Mar 31, 2023
Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

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

I like it 👍

I don't see any drawbacks, and this gives us more flexibility like you said, while also logging the panic in external backends.

rootCmd.cmd.AddCommand(&cobra.Command{
Use: "panic",
RunE: func(cmd *cobra.Command, args []string) error {
panic("oh no, oh no, oh no,no,no,no,no")
Copy link
Contributor

Choose a reason for hiding this comment

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

PTSD dog

errext/exitcodes/codes.go Outdated Show resolved Hide resolved
codebien
codebien previously approved these changes Mar 31, 2023
@na-- na-- dismissed stale reviews from codebien and imiric via cf4fd49 March 31, 2023 10:40
Because we started calling os.Exit() in a defer statement, we circumvented the regular handling of panics that bubble up to the top by the Go runtime. I could have removed the os.Exit() call from the defer, but handling panics ourselves in this way allows us to handle them exactly how we want, assign them their own unique exit code, and test the whole thing with an e2e test.
@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2023

Codecov Report

Merging #2997 (440872c) into master (9a635a5) will increase coverage by 0.03%.
The diff coverage is 71.42%.

❗ Current head 440872c differs from pull request most recent head e4265bb. Consider uploading reports for the commit e4265bb to get more accurate results

@@            Coverage Diff             @@
##           master    #2997      +/-   ##
==========================================
+ Coverage   76.90%   76.93%   +0.03%     
==========================================
  Files         228      228              
  Lines       17043    17050       +7     
==========================================
+ Hits        13107    13118      +11     
+ Misses       3090     3086       -4     
  Partials      846      846              
Flag Coverage Δ
ubuntu 76.93% <71.42%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/root.go 94.26% <71.42%> (-1.07%) ⬇️

... and 8 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@na-- na-- merged commit c09919b into master Mar 31, 2023
20 checks passed
@na-- na-- deleted the fix-panic-handling branch March 31, 2023 11:02
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.

None yet

4 participants