glue
glue copied to clipboard
FR: Error handling when variable is not found: specific error message and class
When using an unknown variable in glue()
, the error message can be a little cryptic:
library(glue)
x=5
glue("x={x}")
#> x=5
glue("w={w}")
#> Error in eval(parse(text = text, keep.source = FALSE), envir): object 'w' not found
Created on 2021-10-24 by the reprex package (v2.0.1)
This is most true when glue()
is used within a package and even more if the pattern is provided by the package user.
A specific message, such as "Could not find variable 'w' when glueing '{pattern}'", would make it much clearer.
Moreover, as read in the book "Advanced R":
One of the challenges of error handling in R is that most functions generate one of the built-in conditions, which contain only a
message
and acall
. That means that if you want to detect a specific type of error, you can only work with the text of the error message. This is error-prone, not only because the message might change over time, but also because messages can be translated into other languages.
Indeed, dealing with user-defined patterns in glue throwing this error would be a million times easier if this error had a specific error subclass that you would be able to check, as rlang::abort(.subclass=)
would do.
Would this be an acceptable feature request?