| 1 |
|
| 2 |
abort <- function(message, ..., body = NULL, class = NULL) { |
| 3 |
|
| 4 |
# create condition object |
| 5 |
cnd <- if (is.character(message)) { |
| 6 |
structure(class = c(class, "error", "condition"), list( |
| 7 |
message = paste(c(message, body), collapse = "\n"), |
| 8 |
meta = list(message = message, body = body), |
| 9 |
... |
| 10 |
)) |
| 11 |
} else if (inherits(message, "condition")) { |
| 12 |
message |
| 13 |
} else { |
| 14 |
stop("internal error: abort called with unexpected message") |
| 15 |
} |
| 16 |
|
| 17 |
# if we were called with a custom condition object not having our meta, |
| 18 |
# just throw it as-is |
| 19 |
if (is.null(cnd$meta)) |
| 20 |
stop(cnd) |
| 21 |
|
| 22 |
# signal the condition, giving calling handlers a chance to run first |
| 23 |
signalCondition(cnd) |
| 24 |
|
| 25 |
# if we got here, then there wasn't any tryCatch() handler on the stack. |
| 26 |
# handle printing of the error ourselves, and then stop with fallback. |
| 27 |
all <- c( |
| 28 |
cnd$meta$body, if (length(cnd$meta$body)) "", |
| 29 |
paste("Error:", paste(cnd$meta$message, collapse = "\n")) |
| 30 |
) |
| 31 |
|
| 32 |
# write error message to stderr, as errors might normally do |
| 33 |
writeLines(all, con = stderr()) |
| 34 |
|
| 35 |
# create the fallback, but 'dodge' the existing error handlers |
| 36 |
fallback <- cnd |
| 37 |
fallback$message <- "" |
| 38 |
class(fallback) <- "condition" |
| 39 |
|
| 40 |
# disable error printing for the empty error |
| 41 |
renv_scope_options(show.error.messages = FALSE) |
| 42 |
|
| 43 |
# now throw the error |
| 44 |
stop(fallback) |
| 45 |
|
| 46 |
} |