If I want to convert a warning to an error typically I would do something like the following.
library(testthat)
foo <- function() {
warning("foo")
}
f <- function() {
tryCatch(warning = function(e) stop(e), foo())
}
f()
#> Error in foo() : foo
However if I then try to use this in testthat we get a strange errors due to missing restarts.
# this works interactively, but not in a reprex
expect_error(f())
#> Warning in foo(): foo
#> Error in invokeRestart("muffleWarning"): no 'restart' 'muffleWarning' found
# this does not
test_that("blah", {
expect_error(f())
})
#> Error: Test failed: 'blah'
#> * no 'restart' 'muffleWarning' found
#> 1: expect_error(f()) at <text>:15
#> 2: quasi_capture(enquo(object), label, capture_error)
#> 3: .capture(act$val <- eval_bare(get_expr(.quo), get_env(.quo)), ...)
#> 4: tryCatch({
#> code
#> NULL
#> }, error = function(e) e)
#> 5: tryCatchList(expr, classes, parentenv, handlers)
#> 6: tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 7: doTryCatch(return(expr), name, parentenv, handler)
#> 8: eval_bare(get_expr(.quo), get_env(.quo))
#> 9: f()
#> 10: tryCatch(warning = function(e) stop(e), foo()) at <text>:7
#> 11: tryCatchList(expr, classes, parentenv, handlers)
#> 12: tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 13: value[[3L]](cond)
#> 14: stop(e) at <text>:7
#> 15: (function (e)
#> {
#> if (getOption("warn") >= 2)
#> return()
#> handled <<- TRUE
#> e$expectation_calls <- frame_calls(11, 5)
#> register_expectation(e)
#> invokeRestart("muffleWarning")
#> })(structure(list(message = "foo", call = foo()), class = c("simpleWarning",
#> "warning", "condition")))
#> 16: invokeRestart("muffleWarning")
#> 17: stop(gettextf("no 'restart' '%s' found", as.character(r)), domain = NA)
If I want to convert a warning to an error typically I would do something like the following.
However if I then try to use this in testthat we get a strange errors due to missing restarts.