r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
498 stars 136 forks source link

Weird error message when rethrowing an error #1619

Open mgirlich opened 1 year ago

mgirlich commented 1 year ago

I found this while working on https://github.com/tidyverse/dbplyr/issues/1186. Unfortunately, you need to install the DatabaseConnector package (https://github.com/OHDSI/DatabaseConnector) to run the reprex.

con <- DatabaseConnector::connect(
  dbms = "postgresql",
  server = server,
  user = user,
  password = password
)
#> Connecting using PostgreSQL driver

tryCatch(
  DBI::dbSendQuery(con, "SELECT 1 + a"),
  error = function(cnd) {
    rlang::abort("Rethrow.", parent = cnd)
  }
)
#> Error in ._jobjRef_dollar(x[["jobj"]], name): no field, method or inner class called 'use_cli_format'

Created on 2023-04-25 with reprex v2.0.2

lionel- commented 1 year ago

There may be a condition class somewhere in the DB stack that throws errors when accessing unknown [[ fields. If that is the case I think that's a design issue with the class.

schuemie commented 1 year ago

This appears to be some interaction between rlang and rJava (DatabaseConnector uses rJava):

tryCatch(
  rJava::J("blah"),
  error = function(cnd) {
    rlang::abort("Rethrow.", parent = cnd)
  }
)
# Error in ._jobjRef_dollar(x[["jobj"]], name) :
#   no field, method or inner class called 'use_cli_format'

So rJava appears to throw errors that are in some way considered illegal by rlang?

lionel- commented 1 year ago

Good find. The restrictions added to error objects by rJava subclasses make it hard to work with generically.