-
Notifications
You must be signed in to change notification settings - Fork 1k
replaced panic() calls with proper error handling in production code #6658
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: master
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 | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |||||
package gclient | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"k8s.io/apimachinery/pkg/runtime" | ||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||||||
"k8s.io/client-go/kubernetes/scheme" | ||||||
|
@@ -70,10 +71,13 @@ func NewForConfig(config *rest.Config) (client.Client, error) { | |||||
|
||||||
// NewForConfigOrDie creates a new client for the given config and | ||||||
// panics if there is an error in the config. | ||||||
// Note: This function is kept for backward compatibility but should be avoided in new code. | ||||||
// Use NewForConfig instead for proper error handling. | ||||||
func NewForConfigOrDie(config *rest.Config) client.Client { | ||||||
c, err := NewForConfig(config) | ||||||
if err != nil { | ||||||
panic(err) | ||||||
// Log the error before panicking for better debugging | ||||||
panic(fmt.Sprintf("failed to create client: %v", err)) | ||||||
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. Using
Suggested change
|
||||||
} | ||||||
return c | ||||||
} |
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
return
statement here only exits the anonymousregister
function, notNewDetectorInitializers
. This means a duplicate detector registration would be silently ignored (apart from the log message), which is likely not the intended behavior. The originalpanic
indicated a fatal error. To maintain this behavior while improving logging, consider usingklog.Fatalf
which logs the error and exits the program.