Home > Back-end >  How can I catch exceptions in any part of code and do something in R?
How can I catch exceptions in any part of code and do something in R?

Time:05-01

I want to call an SMS API whenever a code throws an error. To elaborate, we have a lot of cron jobs on our server and for maintenance I want to be aware when ever one of these codes breaks.

So I want to send my self a SMS whenever an error happens.

I can't use try catch because I don't know which part of code might cause error.

Thanks in advance.

CodePudding user response:

What you are looking for is probably setting up a custom error handler. You can do that by setting option called error, which is NULL by default (unless you are using RStudio).

You can e.g. add such code to your .Rprofile:

options(error = function() {
   your_function_to_send_sms(geterrmessage())
})

geterrmessage() is a function that accesses the last error message. You can check documentation of options and geterrmessage for details.

I personally haven't used that extensively, I've made a simple code once that dumps error to external file under some conditions.

  • Related