Is it possible to catch all errors in a Shiny app?

Viewed 87

I have a Shiny app which is effectively a front-end to an SQL database. The connection to the database can be unreliable, so I've adopted the pool package to handle the connections. However, there are still some rare cases where the connection is lost and the connection pool cannot recover.

This can be solved by using tryCatch to handle such errors: if an error is thrown, I can handle it by restarting the connection. However, this would get very bloated very fast, having to duplicate this code every time I query the database.

Or, to keep my code DRY, I could create a wrapper around all the DBI functions (i.e. a function which calls dbSendQuery() within a tryCatch and handles any errors that pop out) but that would be a hassle.

The simplest solution, in my opinion, would be to have a function which catches all errors. I can then determine whether the error is relevant, refresh the connection, and then simply ask the user to try again. If any other type of error appears, I can display it to the user.

I've seen the shiny::validate and ::need functions, but can't see how to use them or any sort of observe*() method to solve this problem.

So, can this be done in an elegant way?

0 Answers
Related