I need to convert a warning to an error to be able to handle it further upstream (warnings are swallowed somewhere in the middle, over which I have no control; errors are not). To do this, I’m doing the following:
warning_to_error = function (expr)
withCallingHandlers(expr, warning = stop)
This works great:
> warning_to_error(warning('foobar'))
Error in withCallingHandlers(expr, warning = stop) : foobar
Unfortunately, this makes the error completely uncatchable:
> try(warning_to_error(warning('foobar')))
Error in withCallingHandlers(expr, warning = stop) : foobar
In my real situation, there are several layers between my warning_to_error
and the try
(including the logic that muffles warnings). How can I make the error raised by my calling handler catchable? Unfortunately I cannot use restarts as described in another Stack Overflow question because stop
doesn’t define a restart, and I once again have no control over the code doing the catching anyway.