Question

I would like to change the default value of the call. argument in stop() when called form inside my package. Therefore the packages includes a (non exported) version of stop that uses another default:

stop <- function(..., call. = FALSE, domain = NULL){
  base::stop(..., call. = call., domain = domain);
}

This does what I intended for stop("my error"). However when stop is called with a condition object, it gives an additional warning:

> stop(simpleError("foo"))
Error: foo
In addition: Warning message:
 In base::stop(..., call. = call., domain = domain) :
 additional arguments ignored in stop()

The error is raised by r-base in stop.R. How can I write a stop function that behaves exactly the same as base::stop, apart from a different default for the call. argument?

Was it helpful?

Solution

base::stop will always give that warning if you call it with a condition (e.g. caught error from a tryCatch) as its argument and supply call. or domain.

This is because of the check in base::stop

if (length(args) == 1L && inherits(args[[1L]], "condition")) {
    ....
    if (nargs() > 1L) # give the warning

where args is your ....

For the call. argument, stop takes this from the error object itself rather than from the call. argument to stop (hence the warning you are getting)::

stop(simpleError('achtung!'))
# Error: achtung!
stop(simpleError('achtung!'), call.=F) # gives the warning
# Error: achtung!
# In addition: Warning message:
# In stop(simpleError("achtung!"), call. = F) :
#   additional arguments ignored in stop()
stop(simpleError('achtung!', call=call('f')))
# Error in f() : achtung!

Depending on how the error in your particular code is constructed, you can set call to NULL in the creation of the error to suppress it in the call to stop.

You could either:

  • modify your code that calls stop(someConditionObject) to just call stop with that object's message (so that the call is not passed through)
  • modify your stop to test if inherits(args[[1L]], "condition") and set the call to NULL if call.=F to suppress it (error$call <- NULL), and then call base::stop(error) without the call. argument.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top