Skip to content

Commit

Permalink
Lift error handler to chunk level
Browse files Browse the repository at this point in the history
Signed-off-by: eternal-flame-AD <[email protected]>
  • Loading branch information
eternal-flame-AD committed Oct 11, 2024
1 parent 1017891 commit 361aca5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
26 changes: 8 additions & 18 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,24 +259,14 @@ eng_r = function(options) {
} else if (cache.exists && isFALSE(options$cache.rebuild)) {
cache$output(options$hash, 'list')
} else in_input_dir(
withCallingHandlers(
evaluate(
code, envir = env, new_device = FALSE,
keep_warning = if (is.numeric(options$warning)) TRUE else options$warning,
keep_message = if (is.numeric(options$message)) TRUE else options$message,
stop_on_error = if (is.numeric(options$error)) options$error else {
if (options$error && options$include) 0L else 2L
},
output_handler = knit_handlers(options$render, options)
),
error = function (cnd) {
if (xfun::pkg_available('rlang', '1.0.0')) {
rlang::abort(paste0("Error in chunk ", options$label), parent = cnd)
} else {
signalCondition(cnd)
stop(cnd)
}
}
evaluate(
code, envir = env, new_device = FALSE,
keep_warning = if (is.numeric(options$warning)) TRUE else options$warning,
keep_message = if (is.numeric(options$message)) TRUE else options$message,
stop_on_error = if (is.numeric(options$error)) options$error else {
if (options$error && options$include) 0L else 2L
},
output_handler = knit_handlers(options$render, options)
)
)
if (options$cache %in% 1:2 && (!cache.exists || isTRUE(options$cache.rebuild))) {
Expand Down
15 changes: 6 additions & 9 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,14 @@ process_file = function(text, output) {
if (progress && is.function(pb$update)) pb$update(i)
group = groups[[i]]
knit_concord$set(block = i)
res[i] = xfun:::handle_error(
withCallingHandlers(
if (tangle) process_tangle(group) else process_group(group),
error = function(e) if (xfun::pkg_available('rlang', '1.0.0')) rlang::entrace(e)
),
function(loc) {
res[i] = withCallingHandlers(
if (tangle) process_tangle(group) else process_group(group),
error = function(cnd) {
setwd(wd)
write_utf8(res, output %n% stdout())
paste0('\nQuitting from lines ', loc)
},
if (labels[i] != '') sprintf(' [%s]', labels[i]), get_loc
loc <- if (labels[i] != '') get_loc(sprintf(' [%s]', labels[i])) else ''
rlang::abort(paste0('Quitting from lines ', loc), parent = cnd)
}
)
}

Expand Down

0 comments on commit 361aca5

Please sign in to comment.