-
Notifications
You must be signed in to change notification settings - Fork 272
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
map()
and pmap()
remove class from error
#1027
Comments
Yeah, because it's wrapped inside another error now. |
Does that mean this is a behavioural change that's here to stay? I understand the intent of this to be to re-throw the error, but with extra info about the offending element: Line 205 in 54546b6
|
I think we may need to implement in Currently you need to implement this behaviour manually using foo <- function() abort("foo", class = "foo")
# Using `try_fetch()` to easily return a value or ignore the condition
try_fetch(
purrr::map(1, \(x) foo()),
# Inspect all errors
error = function(cnd) {
if (cnd_inherits(cnd, "foo")) {
# We got a match, return a value
class(cnd)
} else {
# Ignore the ones that don't match
zap()
}
}
)
#> [1] "rlang_error" "error" "condition" If try_fetch(
purrr::map(1, \(x) foo()),
foo = function(cnd) class(cnd)
) |
oops, the complicated try_fetch(
purrr::map(1, \(x) foo()),
error = function(cnd) {
repeat {
if (is_null(cnd)) {
return(zap())
}
if (inherits(cnd, "foo")) {
return(class(cnd))
}
cnd <- cnd[["parent"]]
}
}
)
#> [1] "foo" "rlang_error" "error" "condition" |
We’re likely to provide a way out of the error wrapping in a patch release in early Jan. |
Just to be clear, disabling the error wrapping only helps when you don't want the error wrapping, i.e. no index iteration info in error messages. It'd still be helpful to support parent errors in |
I just mentioned this because lionel, point error wrapping being needed is really important. |
I think it'd be helpful to call this out in the NEWS as a breaking change. I think it'd also be worth calling out Code from before purrr 1.0.0 abort_special <- function(x) rlang::abort("my error", class = "error_special")
## purrr <= 0.3.5 ----
tryCatch(
purrr::map(1, abort_special),
error_special = function(cnd) {
message("handling special error...")
cnd
}
)
#> handling special error...
#> <error/error_special>
#> Error in `.f()`:
#> ! my error
#> ---
#> Backtrace:
#> 1. base::tryCatch(...)
#> 5. purrr::map(1, abort_special)
#> 6. global .f(.x[[i]], ...) now becomes the following ## purrr >= 1.0.0 ----
tryCatch(
purrr::map(1, abort_special),
error = function(cnd) {
if (rlang::cnd_inherits(cnd, "error_special")) {
message("handling special error...")
cnd$parent
} else {
stop(cnd)
}
}
)
#> handling special error...
#> <error/error_special>
#> Error in `.f()`:
#> ! my error
#> ---
#> Backtrace:
#> 1. base::tryCatch(...)
#> 5. purrr::map(1, abort_special)
#> 6. purrr:::map_("list", .x, .f, ..., .progress = .progress)
#> 9. global .f(.x[[i]], ...) If the message handler doesn't need to access data in the underlying error (i.e. taking out Edited: the non-special condition has to be rethrown or errors are silently swallowed; thanks for the reminder @hadley. |
Sounds like this issue needs three things:
|
@gadenbuie there's a subtle error in your code. I think you want: tryCatch(
purrr::map(1, abort_special),
error = function(cnd) {
if (rlang::cnd_inherits(cnd$parent, "error_special")) {
message("handling special error...")
cnd$parent
} else {
stop(cnd)
}
}
) Otherwise other errors will be silently swallowed. |
@gadenbuie Also |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@mlane3 please don't |
Closes #406 Relates to tidyverse/purrr#1027
* Adapt to purrr's indexed errors Closes #406 Relates to tidyverse/purrr#1027 * purrr PR has been merged
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 insidepmap()
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 anoops_error
.But as of v1.0.0, the error no longer has that condition.
The text was updated successfully, but these errors were encountered: