Skip to content

Commit

Permalink
Use withCallingHandlers() (#1397)
Browse files Browse the repository at this point in the history
Fixes #1378
  • Loading branch information
hadley authored Nov 8, 2023
1 parent cbe1b99 commit 388a6ee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion R/db-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ db_write_table.DBIConnection <- function(con,
check_bool(temporary)
check_bool(overwrite)

tryCatch(
withCallingHandlers(
dbWriteTable(
con,
name = table_ident_to_id(table),
Expand Down
28 changes: 15 additions & 13 deletions R/db-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ db_analyze.DBIConnection <- function(con, table, ...) {
if (is.null(sql)) {
return() # nocov
}
tryCatch(
withCallingHandlers(
DBI::dbExecute(con, sql),
error = function(cnd) {
msg <- "Can't analyze table {.field {format(table, con = con)}}."
Expand All @@ -1086,7 +1086,7 @@ db_create_index.DBIConnection <- function(con,
unique = FALSE,
...) {
sql <- sql_table_index(con, table, columns, name = name, unique = unique, ...)
tryCatch(
withCallingHandlers(
DBI::dbExecute(con, sql),
error = function(cnd) {
msg <- "Can't create index on table {.field {format(table, con = con)}}."
Expand All @@ -1103,10 +1103,9 @@ dbplyr_explain <- function(con, ...) {
db_explain.DBIConnection <- function(con, sql, ...) {
sql <- sql_query_explain(con, sql, ...)
call <- current_call()
tryCatch(
{
expl <- DBI::dbGetQuery(con, sql)
}, error = function(cnd) {
expl <- withCallingHandlers(
DBI::dbGetQuery(con, sql),
error = function(cnd) {
cli_abort("Can't explain query.", parent = cnd)
}
)
Expand All @@ -1122,10 +1121,9 @@ dbplyr_query_fields <- function(con, ...) {
#' @importFrom dplyr db_query_fields
db_query_fields.DBIConnection <- function(con, sql, ...) {
sql <- sql_query_fields(con, sql, ...)
tryCatch(
{
df <- DBI::dbGetQuery(con, sql)
}, error = function(cnd) {
df <- withCallingHandlers(
DBI::dbGetQuery(con, sql),
error = function(cnd) {
cli_abort("Can't query fields.", parent = cnd)
}
)
Expand All @@ -1144,7 +1142,7 @@ db_save_query.DBIConnection <- function(con,
...,
overwrite = FALSE) {
sql <- sql_query_save(con, sql, name, temporary = temporary, ...)
tryCatch(
withCallingHandlers(
{
if (overwrite) {
name <- as_table_ident(name)
Expand All @@ -1155,8 +1153,12 @@ db_save_query.DBIConnection <- function(con,
}
}
DBI::dbExecute(con, sql, immediate = TRUE)
}, error = function(cnd) {
cli_abort("Can't save query to table {.table {format(name, con = con)}}.", parent = cnd)
},
error = function(cnd) {
cli_abort(
"Can't save query to table {.table {format(name, con = con)}}.",
parent = cnd
)
}
)
name
Expand Down
9 changes: 6 additions & 3 deletions R/rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,7 @@ rows_auto_copy <- function(x, y, copy, call = caller_env()) {

rows_get_or_execute <- function(x, sql, returning_cols, call = caller_env()) {
con <- remote_con(x)
msg <- "Can't modify database table {.val {remote_name(x)}}."
tryCatch(
withCallingHandlers(
{
if (is_empty(returning_cols)) {
DBI::dbExecute(con, sql, immediate = TRUE)
Expand All @@ -771,7 +770,11 @@ rows_get_or_execute <- function(x, sql, returning_cols, call = caller_env()) {
}
},
error = function(cnd) {
cli_abort(msg, parent = cnd, call = call)
cli_abort(
"Can't modify database table {.val {remote_name(x)}}.",
parent = cnd,
call = call
)
}
)

Expand Down
4 changes: 2 additions & 2 deletions R/verb-compute.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ collect.tbl_sql <- function(x, ..., n = Inf, warn_incomplete = TRUE, cte = FALSE
}

sql <- db_sql_render(x$src$con, x, cte = cte)
tryCatch(
out <- db_collect(x$src$con, sql, n = n, warn_incomplete = warn_incomplete, ...),
out <- withCallingHandlers(
db_collect(x$src$con, sql, n = n, warn_incomplete = warn_incomplete, ...),
error = function(cnd) {
cli_abort("Failed to collect lazy table.", parent = cnd)
}
Expand Down

0 comments on commit 388a6ee

Please sign in to comment.