Skip to content

Commit

Permalink
Convert stop_on_error to an enum (#168)
Browse files Browse the repository at this point in the history
This makes it easier for me to understand since I don't need to keep looking up the docs.
  • Loading branch information
hadley authored Jun 21, 2024
1 parent 6f07825 commit 9223025
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
37 changes: 27 additions & 10 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
#' the parent environment to `envir`.
#' @param debug if `TRUE`, displays information useful for debugging,
#' including all output that evaluate captures.
#' @param stop_on_error if `2`, evaluation will halt on first error and you
#' will get no results back. If `1`, evaluation will stop on first error
#' without signaling the error, and you will get back all results up to that
#' point. If `0` will continue running all code, just as if you'd pasted
#' the code into the command line.
#' @param stop_on_error A number between 0 and 2 that controls what happens
#' when the code errors:
#'
#' * If `0`, the default, will continue running all code, just as if you'd
#' pasted the code into the command line.
#' * If `1`, evaluation will stop on first error without signaling the error,
#' and you will get back all results up to that point.
#' * If `2`, evaluation will halt on first error and you will get back no
#' results.
#' @param keep_warning,keep_message whether to record warnings and messages; if
#' `FALSE`, messages will be suppressed; if `NA`, they will not be captured
#' (normally they will be sent to the console). Note that if the environment
Expand Down Expand Up @@ -50,8 +54,8 @@ evaluate <- function(input,
output_handler = NULL,
filename = NULL,
include_timing = FALSE) {
stop_on_error <- as.integer(stop_on_error)
stopifnot(length(stop_on_error) == 1)

on_error <- check_stop_on_error(stop_on_error)

# if this env var is set to true, always bypass messages
if (env_var_is_true('R_EVALUATE_BYPASS_MESSAGES')) {
Expand All @@ -65,7 +69,7 @@ evaluate <- function(input,
warning("`evaluate(include_timing)` is deprecated")
}

parsed <- parse_all(input, filename, stop_on_error != 2L)
parsed <- parse_all(input, filename, on_error != "error")
if (inherits(err <- attr(parsed, 'PARSE_ERROR'), 'error')) {
source <- new_source(parsed$src, expression(), output_handler$source)
output_handler$error(err)
Expand All @@ -90,15 +94,15 @@ evaluate <- function(input,
src = parsed$src[[i]],
watcher = watcher,
envir = envir,
use_try = stop_on_error != 2L,
use_try = on_error != "error",
keep_warning = keep_warning,
keep_message = keep_message,
log_warning = log_warning,
output_handler = output_handler
)
watcher$check_devices()

if (stop_on_error > 0L && watcher$has_errored()) {
if (on_error == "stop" && watcher$has_errored()) {
break
}
}
Expand Down Expand Up @@ -236,3 +240,16 @@ reset_call <- function(cnd) {
}
cnd
}

check_stop_on_error <- function(x) {
if (is.numeric(x) && length(x) == 1 && !is.na(x)) {
if (x == 0L) {
return("continue")
} else if (x == 1L) {
return("stop")
} else if (x == 2L) {
return("error")
}
}
stop("`stop_on_error` must be 0, 1, or 2 ", call. = FALSE)
}
15 changes: 10 additions & 5 deletions man/evaluate.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/eval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# check_stop_on_error converts integer to enum

Code
check_stop_on_error(4)
Condition
Error:
! `stop_on_error` must be 0, 1, or 2

8 changes: 8 additions & 0 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ test_that("multiple lines of comments do not lose the terminating \\n", {
expect_output_types(ev, c("source", "source"))
expect_equal(ev[[1]]$src, "# foo\n")
})

test_that("check_stop_on_error converts integer to enum", {
expect_equal(check_stop_on_error(0), "continue")
expect_equal(check_stop_on_error(1), "stop")
expect_equal(check_stop_on_error(2), "error")

expect_snapshot(check_stop_on_error(4), error = TRUE)
})

0 comments on commit 9223025

Please sign in to comment.