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] tools-v2: fix incorrect error output #2802

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

saicaca
Copy link
Contributor

@saicaca saicaca commented Oct 12, 2023

What problem does this PR solve?

Issue Number: #2801

Problem Summary: Error messages contains character % cannot be output correctly because % is treated as the prefix of a format verb.

What is changed and how it works?

What's Changed: Replaced fmt.Errorf(ce.Message) with errors.New(ce.Message) in ToError()

How it Works: errors.New treats the error messages as plain text instead of format strings.

Side effects(Breaking backward compatibility? Performance regression?):

Check List

  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license

Signed-off-by: saicaca <zephyird@gmail.com>
@saicaca
Copy link
Contributor Author

saicaca commented Oct 12, 2023

cicheck

@@ -69,7 +70,7 @@ func (ce *CmdError) ToError() error {
if ce.Code == CODE_SUCCESS {
return nil
}
return fmt.Errorf(ce.Message)
return errors.New(ce.Message)
Copy link
Contributor

Choose a reason for hiding this comment

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

The effect is the same, why is it modified like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As described in #2801, fmt.Errorf treats the input string as a format string. This can be problematic when the error message contains a % character (For example a web URL with non-ASCII characters). In such cases, the % character is mistakenly treated as the beginning of a format verb, leading to incorrect output. Using errors.New can fix this problem.

To illustrate, the following code snippet

msg := "GET failed: https://example.com?msg=hello%20world"
err1 := fmt.Errorf(msg)
err2 := errors.New(msg)
fmt.Println(err1.Error())
fmt.Println(err2.Error())

outputs

GET failed: https://example.com?msg=hello%!w(MISSING)orld
GET failed: https://example.com?msg=hello%20world

@baytan0720
Copy link
Member

cicheck

Copy link
Contributor

@Cyber-SiKu Cyber-SiKu left a comment

Choose a reason for hiding this comment

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

LGTM!

@Cyber-SiKu Cyber-SiKu merged commit 10a8fd4 into opencurve:master Nov 3, 2023
5 checks passed
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.

3 participants