-
Notifications
You must be signed in to change notification settings - Fork 0
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
💝 Allow error handlers to access the Cobra' command object #4
Conversation
WalkthroughThe changes involve updates to the Changes
Possibly related PRs
Suggested labels
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
app.go (2)
25-25
: LGTM: ErrorHandler signature update enhances functionalityThe addition of the
cmd *cobra.Command
parameter to theErrorHandler
type signature is a good improvement. It allows error handlers to access the Cobra command object, providing more context for error handling. This change aligns well with the PR objective and enhances the flexibility of error handling in the application.Consider renaming the
cmd
parameter tocommand
for improved readability:-type ErrorHandler func(err error, cmd *cobra.Command) bool +type ErrorHandler func(err error, command *cobra.Command) bool
46-46
: LGTM with a suggestion for improvementThe update to pass
a.root
as the second argument toErrorHandler
is consistent with the new type signature. This change allows the error handler to access the command context, which is a good improvement.For multi-command applications, consider passing the specific command that caused the error instead of always passing the root command. This would provide more precise context to the error handler. You might need to modify the
Execute
method of thecobra.Command
to track the current command. Here's a conceptual example of how this could be implemented:func (a *App) Execute(options ...Option) error { // ... existing code ... var currentCmd *cobra.Command a.root.PersistentPreRun = func(cmd *cobra.Command, args []string) { currentCmd = cmd } err := a.root.Execute() if err != nil { return &commandError{err, currentCmd} } return nil } type commandError struct { err error cmd *cobra.Command } func (ce *commandError) Error() string { return ce.err.Error() } // In ExecuteOrDie if a.ErrorHandler == nil || !a.ErrorHandler(err, ce.cmd) { a.defaultErrorHandler(err) }This approach would provide the specific command that caused the error to the error handler, rather than always passing the root command.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- app.go (2 hunks)
- app_test.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
app_test.go (1)
27-29
: LGTM! Consider adding a test case utilizing the new command parameter.The change successfully implements the PR objective by allowing error handlers to access the Cobra command object. The use of
_
for the unused parameter follows Go conventions.To improve test coverage, consider adding a new test case that utilizes the
*cobra.Command
parameter in the error handler. This would ensure that the new functionality is properly tested.Example:
commandline.WithErrorHandler(func(merr error, cmd *cobra.Command) bool { err = merr assert.Assert(t, cmd != nil, "Command should not be nil") assert.Equal(t, "example", cmd.Use) return false }),Let's verify if there are other usages of
WithErrorHandler
that might need updating:✅ Verification successful
Verified! No additional usages of
WithErrorHandler
require changes.The update to
WithErrorHandler
inapp_test.go
is isolated to this test file and does not affect other parts of the codebase. The test change aligns with the PR objective and maintains existing functionality.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for other usages of WithErrorHandler rg --type go 'WithErrorHandler\(' -C 3Length of output: 547
app.go (1)
23-24
: LGTM: Comment update is clear and consistentThe updated comment accurately reflects the changes made to the
ErrorHandler
type signature. It provides clear information about the new parameter (the Cobra command) and specifies that it's the command that threw the error. This addition enhances the documentation and helps users understand the context provided to the error handler.
Allow error handlers to access the Cobra' command object
Summary by CodeRabbit
New Features
Bug Fixes
Tests