Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the call stack on error #5322

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Preserve the call stack on error
Closes #5308
  • Loading branch information
lionel- committed Jun 10, 2020
commit 68b8b8cf0c37a093f5909659227b31bb0143e253
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* cummean() no longer has off-by-one indexing problem (#5287).

* The call stack is preserved on error. This makes it possible to `recover()`
into problematic code called from dplyr verbs (#5308).


# dplyr 1.0.0

## Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion R/arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ arrange_rows <- function(.data, dots) {
#
# revisit when we have something like mutate_one() to
# evaluate one quosure in the data mask
data <- tryCatch({
data <- withCallingHandlers({
transmute(new_data_frame(.data), !!!quosures)
}, error = function(cnd) {
stop_arrange_transmute(cnd)
Expand Down
2 changes: 1 addition & 1 deletion R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ filter_rows <- function(.data, ...) {
mask <- DataMask$new(.data, caller_env())

env_filter <- env()
tryCatch(
withCallingHandlers(
mask$eval_all_filter(dots, env_filter),
simpleError = function(e) {
stop_dplyr(env_filter$current_expression, dots, fn = "filter", problem = conditionMessage(e))
Expand Down
4 changes: 2 additions & 2 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ mutate_cols <- function(.data, ...) {

new_columns <- list()

tryCatch({
withCallingHandlers({
for (i in seq_along(dots)) {
not_named <- (is.null(dots_names) || dots_names[i] == "")

Expand Down Expand Up @@ -287,7 +287,7 @@ mutate_cols <- function(.data, ...) {

# only unchop if needed
if (is.null(result)) {
result <- tryCatch(
result <- withCallingHandlers(
vec_unchop(chunks, rows),
vctrs_error_incompatible_type = function(cnd) {
abort(class = "dplyr:::error_mutate_incompatible_combine", parent = cnd)
Expand Down
5 changes: 2 additions & 3 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ summarise_cols <- function(.data, ...) {
chunks <- vector("list", length(dots))
types <- vector("list", length(dots))

tryCatch({

withCallingHandlers({
# generate all chunks and monitor the sizes
for (i in seq_along(dots)) {
quo <- dots[[i]]
Expand All @@ -227,7 +226,7 @@ summarise_cols <- function(.data, ...) {

mask$across_cache_reset()

result_type <- types[[i]] <- tryCatch(
result_type <- types[[i]] <- withCallingHandlers(
vec_ptype_common(!!!chunks[[i]]),
vctrs_error_incompatible_type = function(cnd) {
abort(class = "dplyr:::error_summarise_incompatible_combine", parent = cnd)
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-arrange.r
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,17 @@ test_that("can arrange() with unruly class", {
quux(data.frame(x = 3:1, dispatched = TRUE))
)
})

test_that("arrange() preserves the call stack on error (#5308)", {
foobar <- function() stop("foo")

stack <- NULL
expect_error(
withCallingHandlers(
error = function(...) stack <<- sys.calls(),
arrange(mtcars, foobar())
)
)

expect_true(some(stack, is_call, "foobar"))
})
14 changes: 14 additions & 0 deletions tests/testthat/test-filter.r
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,17 @@ test_that("can filter() with unruly class", {
quux(data.frame(x = 1:2, dispatched = TRUE))
)
})

test_that("filter() preserves the call stack on error (#5308)", {
foobar <- function() stop("foo")

stack <- NULL
expect_error(
withCallingHandlers(
error = function(...) stack <<- sys.calls(),
filter(mtcars, foobar())
)
)

expect_true(some(stack, is_call, "foobar"))
})
14 changes: 14 additions & 0 deletions tests/testthat/test-mutate.r
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ test_that("can use .before and .after to control column position", {
expect_named(mutate(df, x = 1, .after = y), c("x", "y"))
})

test_that("mutate() preserves the call stack on error (#5308)", {
foobar <- function() stop("foo")

stack <- NULL
expect_error(
withCallingHandlers(
error = function(...) stack <<- sys.calls(),
mutate(mtcars, foobar())
)
)

expect_true(some(stack, is_call, "foobar"))
})

# Error messages ----------------------------------------------------------

test_that("mutate() give meaningful errors", {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-summarise.r
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ test_that("summarise(.groups=)", {

# errors -------------------------------------------------------------------

test_that("summarise() preserves the call stack on error (#5308)", {
foobar <- function() stop("foo")

stack <- NULL
expect_error(
withCallingHandlers(
error = function(...) stack <<- sys.calls(),
summarise(mtcars, foobar())
)
)

expect_true(some(stack, is_call, "foobar"))
})

test_that("summarise() gives meaningful errors", {
verify_output(env = env(global_env()), test_path("test-summarise-errors.txt"), {
"# Messages about .groups="
Expand Down