Wishlist-for-R icon indicating copy to clipboard operation
Wishlist-for-R copied to clipboard

Allow `stopifnot()` to not return the call

Open TimTaylor opened this issue 4 years ago • 0 comments

It would be nice if stopifnot() was consistent with stop() and allowed the call to be suppressed. This is useful as it can be less confusing to users if a nested function errors. I think the patch below enables this behaviour with the addition of a parameter call. as in stop().

Index: src/library/base/R/stop.R
===================================================================
--- src/library/base/R/stop.R   (revision 81041)
+++ src/library/base/R/stop.R   (working copy)
@@ -30,7 +30,7 @@
         .Internal(stop(call., .makeMessage(..., domain = domain)))
 }

-stopifnot <- function(..., exprs, exprObject, local = TRUE)
+stopifnot <- function(..., exprs, exprObject, local = TRUE, call. = TRUE)
 {
     n <- ...length()
     if((has.e <- !missing(exprs)) || !missing(exprObject)) {
@@ -83,7 +83,7 @@
                                     "%s are not all TRUE"),
                            Dparse(cl.i))
          }
-           stop(simpleError(msg, call = if(p <- sys.parent(1L)) sys.call(p)))
+           stop(simpleError(msg, call = if(p <- sys.parent(1L) && isTRUE(call.)) sys.call(p)))
         }
     }
     invisible()

TimTaylor avatar Oct 12 '21 11:10 TimTaylor