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

Errors formatting error messages with brackets #735

Closed
topepo opened this issue Oct 22, 2024 · 1 comment
Closed

Errors formatting error messages with brackets #735

topepo opened this issue Oct 22, 2024 · 1 comment

Comments

@topepo
Copy link
Member

topepo commented Oct 22, 2024

Here's an example from tidymodels/recipes#1380

msg <- "Error in if (df < 0) { : missing blah blah\n"
cli::format_error(msg)
#> Error in glue(str, .envir = .envir, .transformer = transformer, .cli = TRUE, : Expecting '}'

# We patched using
msg <- gsub("(\\{)", "\\1\\1", msg)
msg <- gsub("(\\})", "\\1\\1", msg)
msg
#> [1] "Error in if (df < 0) {{ : missing blah blah\n"
cli::format_error(msg)
#> [1] "Error in if (df < 0) { : missing blah blah"

Created on 2024-10-22 with reprex v2.1.0

Happy to add a PR and tests if you suggest where to add gsub's

@gaborcsardi
Copy link
Member

Don't do the gsub()s, the correct way to solve this is:

msg <- "Error in if (df < 0) { : missing blah blah\n"
cli::format_error("{msg}")

The rule of thumb is to always pass a string literal to cli functions, never a variable from the user (that's actually a security issue as well!) or even an error message.

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

No branches or pull requests

2 participants