Check the following example:
library(testthat)
expect_warning(tryCatch(stop("Error!"), error = function(e) warning(e)))
## Error: tryCatch(stop("Error!"), error = function(e) warning(e)) showed 0 warnings
## In addition: Warning message:
## In doTryCatch(return(expr), name, parentenv, handler) : Error!
Why does testthat say that there was no warnings?
Using the withWarnings function discussed in here also shows no sign of warnings. Why tryCatch does not produce warnings if it was asked for it?
You created nested calls to doTryCatch and withCallingHandlers. The problem is that e is not a character, but a simpleError object (which contains another call to doTryCatch). The following somewhat works, but shows the actual context where the warning was thrown:
tryCatch(stop("Error!"), error = function(e) warning(e[[1]]))
#Warning message:
#In value[[3L]](cond) : Error!
library(testthat)
expect_warning(tryCatch(stop("Error!"), error = function(e) warning(e[[1]])))
#no further output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With