Skip to content

Commit ce11e05

Browse files
committed
Use withCallingHandler when wrapping errors
instead of tryCatch to ensure that the error is handled correctly. This is considered best practice (More information error when `quarto_run()` fails #184).
1 parent 45ef579 commit ce11e05

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

R/quarto.R

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,41 +128,18 @@ quarto_run <- function(
128128
...,
129129
.call = rlang::caller_env()
130130
) {
131-
res <- tryCatch(
132-
{
133-
processx::run(
134-
quarto_bin,
135-
args = args,
136-
echo = echo,
137-
error_on_status = TRUE,
138-
echo_cmd = echo_cmd,
139-
...
140-
)
141-
},
142-
error = function(e) {
143-
msg <- c(x = "Error running quarto cli.")
144-
# if there is an error message from quarto CLI, add it to the message
145-
if (e$stderr != "") {
146-
quarto_error_msg <- xfun::split_lines(e$stderr)
147-
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
148-
msg <- c(
149-
msg,
150-
" " = paste0(rep("-", nchar(msg)), collapse = ""),
151-
quarto_error_msg
152-
)
153-
}
154-
155-
# if `--quiet` has been set, quarto CLI won't report any error (e$stderr will be empty)
156-
# So remind user to run without `--quiet` to see the full error message
157-
if (cli_arg_quiet() %in% args)
158-
msg <- c(
159-
msg,
160-
"i" = "Rerun with `quiet = FALSE` to see the full error message."
161-
)
162-
163-
cli::cli_abort(msg, call = .call, parent = e)
164-
}
131+
res <- withCallingHandlers(
132+
processx::run(
133+
quarto_bin,
134+
args = args,
135+
echo = echo,
136+
error_on_status = TRUE,
137+
echo_cmd = echo_cmd,
138+
...
139+
),
140+
error = function(cnd) wrap_quarto_error(cnd, args, .call = .call)
165141
)
142+
166143
invisible(res)
167144
}
168145

@@ -311,3 +288,32 @@ quarto_binary_sitrep <- function(verbose = TRUE, debug = FALSE) {
311288

312289
return(same_config)
313290
}
291+
292+
293+
wrap_quarto_error <- function(cnd, args, .call = rlang::caller_env()) {
294+
msg <- c(x = "Error running quarto cli.")
295+
# if there is an error message from quarto CLI, add it to the message
296+
if (cnd$stderr != "") {
297+
quarto_error_msg <- xfun::split_lines(cnd$stderr)
298+
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
299+
msg <- c(
300+
msg,
301+
" " = paste0(rep("-", nchar(msg)), collapse = ""),
302+
quarto_error_msg
303+
)
304+
}
305+
306+
# if `--quiet` has been set, quarto CLI won't report any error (cnd$stderr will be empty)
307+
# So remind user to run without `--quiet` to see the full error message
308+
if (cli_arg_quiet() %in% args)
309+
msg <- c(
310+
msg,
311+
"i" = "Rerun with `quiet = FALSE` to see the full error message."
312+
)
313+
314+
cli::cli_abort(
315+
msg,
316+
call = .call,
317+
parent = cnd,
318+
)
319+
}

0 commit comments

Comments
 (0)