Skip to content

Commit def4c72

Browse files
authored
Unwrap purrr's indexed errors (#306)
1 parent 6f21565 commit def4c72

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* New `future_map_vec()`, `future_map2_vec()`, `future_pmap_vec()`, and
1717
`future_imap_vec()` to align with purrr (#261).
1818

19+
* When a furrr function errors, purrr's error index is no longer confusingly
20+
reported (#250).
21+
1922
* furrr now looks up the purrr mapping function on the worker itself, rather
2023
than sending over its own copy of the function. This avoids possible issues
2124
when you have, say, purrr 1.0.0 locally but purrr 0.3.5 on the worker, where

R/template.R

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ furrr_template <- function(
438438
poll_progress(futures, file, n)
439439
}
440440

441-
values <- future::value(futures)
441+
values <- furrr_try_catch({
442+
future::value(futures)
443+
})
442444

443445
if (length(values) != length(chunks)) {
444446
abort("Internal error: Length of `values` not equal to length of `chunks`.")
@@ -460,6 +462,30 @@ furrr_template <- function(
460462

461463
# ------------------------------------------------------------------------------
462464

465+
furrr_try_catch <- function(expr) {
466+
tryCatch(
467+
expr = expr,
468+
purrr_error_indexed = rethrow_purrr_error_indexed
469+
)
470+
}
471+
472+
rethrow_purrr_error_indexed <- function(cnd) {
473+
# The purrr index information doesn't align with furrr's since the
474+
# input is chunked along the workers. We rethrow the parent instead,
475+
# which is the original error.
476+
cnd <- cnd$parent
477+
478+
if (!is_condition(cnd)) {
479+
# If something changes in purrr and `$parent` doesn't exist, just
480+
# return and let normal erroring take its course
481+
return()
482+
}
483+
484+
rlang::cnd_signal(cnd)
485+
}
486+
487+
# ------------------------------------------------------------------------------
488+
463489
make_expr_seed_setup <- function(seed) {
464490
if (is.null(seed) || is_false(seed)) {
465491
return(NULL)

tests/testthat/_snaps/future-map.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,43 @@
123123
Error in `future_map_vec()`:
124124
! Can't combine `<output>[[1]]` <double> and `<output>[[2]]` <character>.
125125

126+
# errors don't report purrr's indices (#250) / strategy - sequential / cores - 1
127+
128+
Code
129+
future_map(x, fail_on_five)
130+
Condition
131+
Error in `...furrr_fn()`:
132+
! Failure!
133+
134+
# errors don't report purrr's indices (#250) / strategy - multisession / cores - 1
135+
136+
Code
137+
future_map(x, fail_on_five)
138+
Condition
139+
Error in `...furrr_fn()`:
140+
! Failure!
141+
142+
# errors don't report purrr's indices (#250) / strategy - multisession / cores - 2
143+
144+
Code
145+
future_map(x, fail_on_five)
146+
Condition
147+
Error in `...furrr_fn()`:
148+
! Failure!
149+
150+
# errors don't report purrr's indices (#250) / strategy - multicore / cores - 1
151+
152+
Code
153+
future_map(x, fail_on_five)
154+
Condition
155+
Error in `...furrr_fn()`:
156+
! Failure!
157+
158+
# errors don't report purrr's indices (#250) / strategy - multicore / cores - 2
159+
160+
Code
161+
future_map(x, fail_on_five)
162+
Condition
163+
Error in `...furrr_fn()`:
164+
! Failure!
165+

tests/testthat/test-future-map.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,18 @@ furrr_test_that("`...` globals/packages are found", {
304304
list(1, 1)
305305
)
306306
})
307+
308+
furrr_test_that("errors don't report purrr's indices (#250)", {
309+
fail_on_five <- function(x) {
310+
if (x == 5) {
311+
stop("Failure!")
312+
}
313+
1L
314+
}
315+
316+
x <- 1:20
317+
318+
expect_snapshot(error = TRUE, {
319+
future_map(x, fail_on_five)
320+
})
321+
})

0 commit comments

Comments
 (0)