In a package, I have a main function that calls a lot of other, unexported functions, with a lot of condition checks everywhere.
When using tidyverse error handling, for instance using the awesome {cli} package, you can attach a caller environment that will be used to decorate the function. The documentation is available here.
Here is some example code:
main = function() f1()
f1 = function() f2()
f2 = function() cli::cli_abort("foobar", call=rlang::caller_env())
main()
#> Error in `f1()`:
#> ! foobar
Created on 2022-07-03 by the reprex package (v2.0.1)
The caller is not f2() but its parent f1(), but the error is still not informative as the end-user is not aware of the internal f1() function either.
To have main() as the end caller, I can either write call=rlang::caller_env(2), which is not very maintainable, or pass the caller as an argument in each and every function that stack, which would require a heavy refactor.
Is there a way to have a global variable that would act as a reference to main(), and which I could use anywhere in my code?