Skip to content

FR: Error handling when variable is not found: specific error message and class #229

@DanChaltiel

Description

@DanChaltiel

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 a call. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions