Skip to content

map() and pmap() remove class from error #1027

@jennybc

Description

@jennybc

I noticed this when googledrive had a test failure, where the function in question uses a tryCatch():

https://github.com/tidyverse/googledrive/blob/7e8fa7abcb220566d2e1e99621b6e5d41e8f1c5b/R/shortcut.R#L184-L187

This resolve_one() function ultimately gets called inside pmap() and as of v1.0.0, the error no longer has the expected class.


With purrr 0.3.5, the class of an error thrown by .f is preserved. Here it's an oops_error.

f <- function(x) {
  if (x) {
    cli::cli_abort("oops", class = "oops_error")
  }
  x
}
f(FALSE)
#> [1] FALSE
f(TRUE)
#> Error in `f()`:
#> ! oops

#> Backtrace:
#>     ▆
#>  1. └─global f(TRUE)
#>  2.   └─cli::cli_abort("oops", class = "oops_error")
#>  3.     └─rlang::abort(...)

packageVersion("purrr")
#> [1] '0.3.5'

purrr::map(c(FALSE, TRUE), f)
#> Error in `.f()`:
#> ! oops

#> Backtrace:
#>     ▆
#>  1. └─purrr::map(c(FALSE, TRUE), f)
#>  2.   └─global .f(.x[[i]], ...)
#>  3.     └─cli::cli_abort("oops", class = "oops_error")
#>  4.       └─rlang::abort(...)
err <- rlang::catch_cnd(purrr::map(c(FALSE, TRUE), f))
err
#> <error/oops_error>
#> Error in `.f()`:
#> ! oops
#> ---
#> Backtrace:
#>  1. rlang::catch_cnd(purrr::map(c(FALSE, TRUE), f))
#>  8. purrr::map(c(FALSE, TRUE), f)
#>  9. global .f(.x[[i]], ...)
class(err)
#> [1] "oops_error"  "rlang_error" "error"       "condition"

But as of v1.0.0, the error no longer has that condition.

f <- function(x) {
  if (x) {
    cli::cli_abort("oops", class = "oops_error")
  }
  x
}
f(FALSE)
#> [1] FALSE
f(TRUE)
#> Error in `f()`:
#> ! oops

#> Backtrace:
#>     ▆
#>  1. └─global f(TRUE)
#>  2.   └─cli::cli_abort("oops", class = "oops_error")
#>  3.     └─rlang::abort(...)

packageVersion("purrr")
#> [1] '1.0.0'

purrr::map(c(FALSE, TRUE), f)
#> Error in `purrr::map()`:
#> ℹ In index: 2.
#> Caused by error in `.f()`:
#> ! oops

#> Backtrace:
#>     ▆
#>  1. └─purrr::map(c(FALSE, TRUE), f)
#>  2.   └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  3.     ├─purrr:::with_indexed_errors(...)
#>  4.     │ └─base::withCallingHandlers(...)
#>  5.     └─global .f(.x[[i]], ...)
#>  6.       └─cli::cli_abort("oops", class = "oops_error")
#>  7.         └─rlang::abort(...)
err <- rlang::catch_cnd(purrr::map(c(FALSE, TRUE), f))
err
#> <error/rlang_error>
#> Error in `purrr::map()`:
#> ℹ In index: 2.
#> Caused by error in `.f()`:
#> ! oops
#> ---
#> Backtrace:
#>   1. rlang::catch_cnd(purrr::map(c(FALSE, TRUE), f))
#>   8. purrr::map(c(FALSE, TRUE), f)
#>   9. purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  12. global .f(.x[[i]], ...)
class(err)
#> [1] "rlang_error" "error"       "condition"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions