Skip to content

[fix] tools-v2: fix incorrect error output#2802

Merged
Cyber-SiKu merged 1 commit intoopencurve:masterfrom
saicaca:toerror-fix
Nov 3, 2023
Merged

[fix] tools-v2: fix incorrect error output#2802
Cyber-SiKu merged 1 commit intoopencurve:masterfrom
saicaca:toerror-fix

Conversation

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

saicaca commented Oct 12, 2023

cicheck

return nil
}
return fmt.Errorf(ce.Message)
return errors.New(ce.Message)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

cicheck

Copy link
Copy Markdown
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
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