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

bug fix/1747. enable error logging for invalid flags #1753

Merged
merged 7 commits into from
Mar 30, 2024

Conversation

ethanwater
Copy link
Contributor

Related Issue

Closes: #1747

Describe the changes you've made

  • Remove the silencing of Record.
  • Implement proper handling of ValidateFlags() calls.

Type of change

  • My code follows the code style of this project.
  • [x ] Bug fix (non-breaking change which fixes an issue)
  • [x ] New feature (non-breaking change which adds functionality)
  • [x ] Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Signed-off-by: Ethan Water <angelshatepop@gmail.com>
Signed-off-by: Ethan Water <angelshatepop@gmail.com>
Copy link

github-actions bot commented Mar 28, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

Copy link

@github-actions github-actions bot left a 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

@ethanwater
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@ethanwater
Copy link
Contributor Author

recheck

@shivamsouravjha
Copy link
Contributor

do these fixes work, I tried the same them locally didn't work for me
image

if possible attach the screenshots

@ethanwater
Copy link
Contributor Author

ethanwater commented Mar 29, 2024

@shivamsouravjha , it works.

Screenshot 2024-03-29 at 11 08 44

The code also works if the long variant of an incorrect flag is used, ex: --applekepapa.

Screenshot 2024-03-29 at 11 14 40

I've reviewed the code, and it properly extinguishes the issue.

cli/config.go Outdated
Comment on lines 27 to 31
if err := cmdConfigurator.ValidateFlags(ctx, cmd); err != nil {
utils.LogError(logger, err, "failed to validate flags")
return err
}
return nil
Copy link
Contributor

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?

Copy link
Contributor Author

@ethanwater ethanwater Mar 29, 2024

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.

Copy link
Contributor Author

@ethanwater ethanwater Mar 29, 2024

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.

Copy link
Contributor

@shivamsouravjha shivamsouravjha Mar 29, 2024

Choose a reason for hiding this comment

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

I understand the use of said log function but they're not leading to expected results, I put a print function in utils.LogError function and it doesn't work. I wasn't able to see the said print statement as well as the logs.

Perhaps you can dig in more as to why this is happening

Eg:
image
image

Copy link
Contributor Author

@ethanwater ethanwater Mar 29, 2024

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.

Copy link
Member

@slayerjain slayerjain Mar 29, 2024

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 :

  1. There seems to some redundancy in the code. eg: utils.LogError(logger, err, "failed to validate flags") is added to both the record and test cmds. It would be better to add the log in one place (maybe somewhere inside the ValidateFlags method? ).
  2. 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...)

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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 :)

Copy link
Contributor Author

@ethanwater ethanwater Mar 29, 2024

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

@nehagup nehagup requested a review from slayerjain March 29, 2024 17:48
Signed-off-by: Ethan Water <watermonic@gmail.com>
Signed-off-by: Ethan Water <watermonic@gmail.com>
@ethanwater
Copy link
Contributor Author

ethanwater commented Mar 29, 2024

@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.

@@ -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 {
Copy link
Member

@slayerjain slayerjain Mar 30, 2024

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 {
Copy link
Member

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.

Copy link
Member

@slayerjain slayerjain left a 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))
Copy link
Member

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>
@ethanwater
Copy link
Contributor Author

@slayerjain Thanks again for the review! And I'm glad you like the new output! I have applied the changes you recommended.

@slayerjain
Copy link
Member

@ethanwater Thanks for the contribution and patience. Looking forward to your next contribution :)

@slayerjain slayerjain merged commit 16eacd7 into keploy:main Mar 30, 2024
14 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 30, 2024
@ethanwater ethanwater deleted the fix/1747 branch March 30, 2024 06:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature]: log error in case of invalid flags
3 participants