[Rd] message(<cond>) and warning(<cond>) circumvent calling handlers and signal the original class, e.g. an error

Andreas Kersting r-deve| @end|ng |rom @ker@t|ng@de
Tue Mar 1 20:05:27 CET 2022


Hi,

There is the same issue with stop():

> w <- simpleWarning("careful")
> tryCatch(stop(w), condition = identity)
<simpleWarning: careful>

I very recently stumbled upon this, when a warning was re-raised as an error, which was then not caught by an outer try():

> try(
+   tryCatch(warning("careful"), warning = function(w) stop(w)),
+   silent = TRUE
+ )
Error in doTryCatch(return(expr), name, parentenv, handler) : careful

I would also like to see this behavior changed. I think that stop() should always signal an error, warning() a warning and message() a message.

Best,
Andreas

2022-03-01 19:38 GMT+01:00 "Henrik Bengtsson" <henrik.bengtsson using gmail.com>:
> Hi, in help("message", package = "base"), we can read:
> 
> Description: 'message' is used for generating 'simple' diagnostic
> messages which are neither warnings nor errors, but nevertheless
> represented as conditions.
> 
> From this, I conclude that message() should generate a condition that
> are neither warning nor errors.
> 
> However, the following signals a condition of class 'error':
> 
>> e <- simpleError("boom!\n")
>> message(e)
> boom!
> 
> This can be seen if we do:
> 
>> res <- tryCatch(message(e), condition = identity)
>> res
> <simpleError: boom!
> 
> This stems from message(e) using signalCondition(e) internally.
> 
> Another problem with this behavior is that message(e) cannot be suppressed:
> 
>> suppressMessages(message(e))
> boom!
> 
> or captured with calling handlers, e.g.
> 
>> res <- withCallingHandlers(message(e), condition = identity)
> boom!
>> res
> NULL
> 
> If we replace e <- simpleError("boom") with e <-
> simpleWarning("careful"), we see a similar behavior.  These problems
> exist also with warning(e).  The current behaviors prevent functions
> from capturing and relaying message(<error>), message(<warning>), and
> warning(<error>).
> 
> I'm happy to post a bug report to <https://bugs.r-project.org/>.
> 
> /Henrik
> 
> PS. BTW, it looks like some recent "..." tweaks to the warning() and
> stop() code could be applied also to message().
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


More information about the R-devel mailing list