-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
bug fix/1747. enable error logging for invalid flags #1753
Conversation
Signed-off-by: Ethan Water <angelshatepop@gmail.com>
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
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.
Thank you and congratulations 🎉 for opening your very first pull request in keploy
I have read the CLA Document and I hereby sign the CLA |
recheck |
@shivamsouravjha , it works. ![]() The code also works if the long variant of an incorrect flag is used, ex: --applekepapa. ![]() I've reviewed the code, and it properly extinguishes the issue. |
cli/config.go
Outdated
if err := cmdConfigurator.ValidateFlags(ctx, cmd); err != nil { | ||
utils.LogError(logger, err, "failed to validate flags") | ||
return err | ||
} | ||
return nil |
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.
what's the use of logerror?
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.
This is proper use of handling errors according to Google's official Golang documentation. The purpose of it is to simply log errors following production standard.
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.
Hopefully this helps you understand: https://go.dev/blog/error-handling-and-go
The same procedure is used at AWS and other established cloud-based companies using Go. I don't understand where your confusion lies.
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.
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 LogError is a function that is used throughout the entirety of the codebase and was designed by the developers.
**What you're looking at is outside the scope of the issue you posted. The but has been fixed and is ready to be merged. **
Also to trigger that, you would need to make ValidateFlags fail. The function is implemented in numerous instances.
If you have further questions about LogError() Ask the creators of Keploy themselves.
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.
@shivamsouravjha it seems to be working fine for me. Please check if there is any env related issue.
@ethanwater thanks for the PR, It does work of me as you described. My initial thoughts :
- There seems to some redundancy in the code. eg:
utils.LogError(logger, err, "failed to validate flags")
is added to both therecord
andtest
cmds. It would be better to add the log in one place (maybe somewhere inside theValidateFlags
method? ). - I personally feel that the error is not standing out enough. It seems to get lost with the usage instructions logged right after it. Would love to listen to some of your ideas on this. Some quick things I can think of is:
- Add a new line after the error.
- Maybe add an error emoji? like ❌or
⚠️ or something more fitting? - Add yellow or red color to the ERROR text? This can be good for console output but doesn't work where ANSI color is not supported. eg: piping to a file, downloading logs from a CI (GHA, Jenkins...)
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.
@ethanwater @shivamsouravjha its totally cool to use the LogError
wrapper. It's something we recently added to avoid logging unnecessary errors while ctx is cancelled. We might add more validations in the future.
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.
Thank you @slayerjain for the review. I can implement these features. Should I implement these in the current PR or another one after merge?
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.
I would really appreciate implementing in the current PR. Happy to actively share my thoughts if you need :)
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.
@slayerjain Awesome, looking at it- I agree with these features. What do you think of this?
![Screenshot 2024-03-29 at 16 38 58](https://private-user-images.githubusercontent.com/124078937/318135408-a0c8e9a0-2fe4-4479-9aad-e1033b9438da.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkzNzg5NjYsIm5iZiI6MTcxOTM3ODY2NiwicGF0aCI6Ii8xMjQwNzg5MzcvMzE4MTM1NDA4LWEwYzhlOWEwLTJmZTQtNDQ3OS05YWFkLWUxMDMzYjk0MzhkYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjI2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyNlQwNTExMDZaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01N2U2OGRhMjJmNGMwN2VmZWVhNDA5NmVmMjAxMjhjOThkYzRjZmI2ZDE3NTM1ZmZlYjZiOGFkMzhmODZjZTRmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.WWoT1xBDJnjOBm8Lnl5hsPsDr2zgg7o1kAIc79ly0Hw)
Signed-off-by: Ethan Water <watermonic@gmail.com>
Signed-off-by: Ethan Water <watermonic@gmail.com>
@slayerjain I implemented the features you requested! I also removed the redundant logging as the previous logging is already substantial enough. Note: I set the revamped error output in the AddFlags function due to it seeming to require the command.SetFlagErrorFunc() being called post initialization. |
cli/provider/cmd.go
Outdated
@@ -153,6 +154,15 @@ func NewCmdConfigurator(logger *zap.Logger, config *config.Config) *CmdConfigura | |||
} | |||
|
|||
func (c *CmdConfigurator) AddFlags(cmd *cobra.Command) error { | |||
//sets the displayment of flag-related errors | |||
cmd.SilenceErrors = true | |||
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { |
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.
@ethanwater cmd variable here is unused, maybe replace with _
cli/config.go
Outdated
@@ -24,10 +24,12 @@ func Config(ctx context.Context, logger *zap.Logger, cfg *config.Config, service | |||
Short: "manage keploy configuration file", | |||
Example: "keploy config --generate --path /path/to/localdir", | |||
PreRunE: func(cmd *cobra.Command, _ []string) error { | |||
return cmdConfigurator.ValidateFlags(ctx, cmd) | |||
if err := cmdConfigurator.ValidateFlags(ctx, cmd); err != nil { |
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.
since we are not doing anything here, it can be left unchanged to return cmdConfigurator.ValidateFlags(ctx, cmd)
as in the previous commit.
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.
This looks awesome Please just remove the redundant error check in the 3 commands as pointed by the linter too and the redundant cmd
variable in cmd.SetFlagErrorFunc
. We should be good to roll after that :)
//sets the displayment of flag-related errors | ||
cmd.SilenceErrors = true | ||
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { | ||
color.Red(fmt.Sprintf("❌ error: %v", err)) |
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.
@ethanwater This is very nice! I really like the output now.
Signed-off-by: Ethan Water <watermonic@gmail.com>
Signed-off-by: Ethan Water <watermonic@gmail.com>
@slayerjain Thanks again for the review! And I'm glad you like the new output! I have applied the changes you recommended. |
@ethanwater Thanks for the contribution and patience. Looking forward to your next contribution :) |
Related Issue
Closes: #1747
Describe the changes you've made
Type of change