mllg / checkmate

Fast and versatile argument checks
https://mllg.github.io/checkmate/
Other
261 stars 30 forks source link

`reportAssertions` always reports errors from top level #238

Open dgkf opened 1 year ago

dgkf commented 1 year ago

I'm not sure if this is intentional, but it's a bit confusing.

Raising assertions in x, I would expect that the error message's call originates from x:

x <- function(a) {
  coll <- makeAssertCollection()
  coll$push("whoops!")
  reportAssertions(coll)
}

y <- function(...) x(...)

y()
## Error in y() : 1 assertions failed:
##  * whoops!

Instead, the error is reported as originating in y().

This occurs because the top level call (sys.call(1L)) is used for the error condition instead of the calling frame (sys.call(-1L))

https://github.com/mllg/checkmate/blob/5e35f452229b2a37536a4e21e1a6b6763fa45dc3/R/AssertCollection.R#L57

For call stacks that span multiple packages, this can give the impression that errors are raised through the surface-level package when they were actually raised by a dependency.

Whether this is intended or not, the option to specify a call provided to reportAssertions would be a nice addition in order to make the error reporting communicate the location where the assertions are raised.