I have the following function:
fun = function(expr) {
mc = match.call()
env = as.environment(within(
list(),
expr = eval(mc$expr)
))
return(env)
}
that gets called within a tryCatch()
so that any error conditions in expr
are handled gracefully.
It works fine with a standard error condition:
tryCatch({
fun({
stop('error')
})
}, error = function(e) {
message('error happened')
})
# error happened
However, it does not capture testthat
expectation errors (which is preferred for my specific use case):
library(testthat)
tryCatch({
fun({
expect_true(FALSE)
})
}, error = function(e) {
message('expectation not met')
})
# Error: FALSE isn't true.
or more simply:
library(testthat)
tryCatch({
expect_true(FALSE)
}, error = function(e) {
message('expectation not met')
})
# Error: FALSE isn't true.
The expectation error is not caught.
This issue appeared after upgrading from R 3.2.2 to R 3.3.0 - i.e. expectation errors were caught just fin in R 3.2.2.
Is there a way to make testthat
expectations caught by tryCatch()
in R 3.3.0?
expect_xxx
functions callexpect
which in turn callswithRestarts
. I don't know exactly what that does but it seems to be the root of the issue you're having. – Giacopo