-
Notifications
You must be signed in to change notification settings - Fork 8
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
.throw_error() and .throw_warning() #254
Comments
Unfortunately, playing around with After much experimenting, I'm thinking that we should simply accept the function name as first argument : .throw_error <- function(fn, ...) {
stop("[", fn, "()] ", ..., call. = FALSE)
} This removes all complexity and guarantees that we always return the correct name. It's still better and allows for more consistent results than using ## set function name for .throw_error/.throw_warning # <- these lines would appear just
fn <- "get_RLum" # <- once at the start of the function
<...>
.throw_error(fn, "'subset' must contain a logical expression") We could stick with using |
There's a problem with this approach: an internal function such as I'm now thinking that every time we enter a function, we could store the function name inside the So whenever a function is defined, we do: .set_function_name("name_of_function_being_defined")
<...>
.throw_error("error message") where the internal functions are: .set_function_name <- function(name) {
.LuminescenceEnv$fn <- name
}
.throw_error <- function(...) {
stop("[", .LuminescenceEnv$fn, "()] ", ..., call. = FALSE)
} Overall this is a much less invasive change that seems to work in the situations I've encountered so far. I'll keep exploring and testing this to see if this is safe and reliable. |
Thanks @mcol , perhaps that is the best idea to invoke this and to not further bother. It was just an idea, but apparently it is currently too complicated to figure out the function name otherwise. |
The last approach I proposed needed some tweaking for functions that self-call or call other functions, such as in this case: inner <- function(x) {
.set_function_name("inner")
}
outer <- function(x) {
.set_function_name("outer")
if (x == "before") .throw_error("before")
inner(x)
if (x == "after") .throw_error("after")
}
outer("before")
# Error: [outer()] before
outer("after")
# Error: [inner()] after # <- this is wrong But anyway, I have a working solution based on setting |
In 0.9.25
.throw_error()
and.throw_warning()
would sometimes choose the wrong frame in looking for the name of the calling function to report. This could happen when the throwing functions were nested in anlapply()
,do.call()
or similar andnframe
was not correctly set. However, this was guaranteed to happen when a function was called via an S3 method, as in that case thenframe
specified in the real function would not account for the fact that that function was called via S3 dispatch.In those cases we reported something like this:
The changes in 57c549a have fixed such cases, but now the code in those two functions is much more complicated and it can result in "unknown()" being reported:
We need to bypass the automatic resolution of the name of calling function.
The text was updated successfully, but these errors were encountered: