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 condition object in a miraiError #176

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Upgrades `stop_mirai()` to cancel remote mirai tasks when using the new dispatcher, returning a logical value indicating whether cancellation was successful.
* Adds `stop_daemon()` to reduce the number of connected daemons (without interrupting any executing tasks).
* `daemon()` gains the new argument 'dispatcher', which should be set to `TRUE` when connecting to dispatcher and `FALSE` when connecting directly to host.
* A 'miraiError' now preserves the original condition object. This means that `rlang::abort()` custom metadata may now be accessed using `$` on the 'miraiError' (thanks @James-G-Hill #173).

#### Updates

Expand Down
6 changes: 3 additions & 3 deletions R/mirai.R
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ print.miraiInterrupt <- function(x, ...) {
#' @export
#'
`$.miraiError` <- function(x, name)
attr(x, name, exact = FALSE)
attr(x, name, exact = TRUE)

#' @exportS3Method utils::.DollarNames
#'
.DollarNames.miraiError <- function(x, pattern = "")
if (startsWith("stack.trace", pattern)) "stack.trace" else character()
grep(pattern, names(attributes(x)), value = TRUE, fixed = TRUE)

# internals --------------------------------------------------------------------

Expand Down Expand Up @@ -628,7 +628,7 @@ mk_mirai_error <- function(e, sc) {
msg <- if (is.null(call) || call == "eval(._mirai_.[[\".expr\"]], envir = ._mirai_., enclos = .GlobalEnv)")
sprintf("Error: %s", .subset2(e, "message")) else
sprintf("Error in %s: %s", call, .subset2(e, "message"))
cat(sprintf("%s\n", msg), file = stderr())
attributes(msg) <- e
idx <- which(as.logical(lapply(sc, identical, quote(eval(._mirai_.[[".expr"]], envir = ._mirai_., enclos = .GlobalEnv)))))
sc <- sc[(length(sc) - 1L):(idx + 1L)]
if (sc[[1L]][[1L]] == ".handleSimpleError")
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_print(r)
test_true(is_mirai_error(r <- `class<-`("Error in: testing\n", c("miraiError", "errorValue", "try-error"))))
test_print(r)
test_null(r$stack.trace)
test_equal(mirai:::.DollarNames.miraiError(NULL, "s"), "stack.trace")
test_equal(mirai:::.DollarNames.miraiError(r, "c"), "class")
test_true(mirai:::is.promising.mirai())
test_error(everywhere({}), "not found")
test_null(nextstream())
Expand Down
Loading