Skip to content

Commit

Permalink
Return output from tryCatch()
Browse files Browse the repository at this point in the history
This ensures that the value is set everytime, not just when there's no error. Fixes #145
  • Loading branch information
hadley committed Jun 17, 2024
1 parent b2a2689 commit 495cfa9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
15 changes: 8 additions & 7 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ evaluate_call <- function(call,
invokeRestart("muffleMessage")
}

ev <- list(value = NULL, visible = FALSE)

if (use_try) {
handle <- function(f) try(f, silent = TRUE)
handle <- function(code) {
tryCatch(code, error = function(err) {
list(value = NULL, visible = FALSE)
})
}
} else {
handle <- force
}
Expand All @@ -252,8 +254,8 @@ evaluate_call <- function(call,
multi_args <- length(formals(value_handler)) > 1
for (expr in call) {
srcindex <- length(output)
time <- timing_fn(handle(
ev <- withCallingHandlers(
time <- timing_fn(ev <- handle(
withCallingHandlers(
withVisible(eval_with_user_handlers(expr, envir, enclos, user_handlers)),
warning = wHandler,
error = eHandler,
Expand All @@ -266,11 +268,10 @@ evaluate_call <- function(call,

# If visible or the value handler has multi args, process and capture output
if (ev$visible || multi_args) {
pv <- list(value = NULL, visible = FALSE)
value_fun <- if (multi_args) value_handler else {
function(x, visible) value_handler(x)
}
handle(pv <- withCallingHandlers(withVisible(
pv <- handle(withCallingHandlers(withVisible(
value_fun(ev$value, ev$visible)
), warning = wHandler, error = eHandler, message = mHandler))
handle_output(TRUE)
Expand Down
3 changes: 3 additions & 0 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ render <- function(x) if (isS4(x)) methods::show(x) else print(x)
#' @param value Function to handle the values returned from evaluation. If it
#' only has one argument, only visible values are handled; if it has more
#' arguments, the second argument indicates whether the value is visible.
#'
#' If the expression errored and `stop_on_error` is not `2`, value
#' will be set to `NULL` and visible will be set to `FALSE`.
#' @param calling_handlers List of [calling handlers][withCallingHandlers].
#' These handlers have precedence over the exiting handler installed
#' by [evaluate()] when `stop_on_error` is set to 0.
Expand Down
5 changes: 4 additions & 1 deletion man/new_output_handler.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/test-evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ test_that("file with only comments runs", {
expect_equal(classes(ev), c("source", "source"))
})

test_that("error after output works correctly", {
ev <- evaluate::evaluate("1;stop('x')")
expect_equal(classes(ev), c("source", "character", "simpleError"))
})

test_that("data sets loaded", {
skip_if_not_installed("lattice")

Expand Down

0 comments on commit 495cfa9

Please sign in to comment.