Skip to content

Commit 850a66e

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 050a590 commit 850a66e

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

R/quarto.R

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,51 +138,19 @@ quarto_run <- function(
138138
if (!quarto_available(min = "1.8.13")) {
139139
custom_env <- c("current", QUARTO_R = R.home("bin"))
140140
}
141-
res <- tryCatch(
142-
{
143-
processx::run(
144-
quarto_bin,
145-
args = args,
146-
echo = echo,
147-
error_on_status = TRUE,
148-
echo_cmd = echo_cmd,
149-
env = custom_env,
150-
...
151-
)
152-
},
153-
error = function(e) {
154-
if (!inherits(e, "system_command_status_error")) {
155-
cli::cli_abort(
156-
c("!" = "Error running quarto CLI from R."),
157-
call = .call,
158-
parent = e
159-
)
160-
} else {
161-
msg <- c(x = "Error returned by quarto CLI.")
162-
# if there is an error message from quarto CLI, add it to the message
163-
if (e$stderr != "") {
164-
quarto_error_msg <- xfun::split_lines(e$stderr)
165-
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
166-
msg <- c(
167-
msg,
168-
" " = paste0(rep("-", nchar(msg)), collapse = ""),
169-
quarto_error_msg
170-
)
171-
}
172-
173-
# if `--quiet` has been set, quarto CLI won't report any error (e$stderr will be empty)
174-
# So remind user to run without `--quiet` to see the full error message
175-
if (cli_arg_quiet() %in% args) {
176-
msg <- c(
177-
msg,
178-
"i" = "Rerun with `quiet = FALSE` to see the full error message."
179-
)
180-
}
181-
182-
cli::cli_abort(msg, call = .call, parent = e)
183-
}
184-
}
141+
res <- withCallingHandlers(
142+
processx::run(
143+
quarto_bin,
144+
args = args,
145+
echo = echo,
146+
error_on_status = TRUE,
147+
echo_cmd = echo_cmd,
148+
env = custom_env,
149+
...
150+
),
151+
error = function(e) wrap_quarto_error(e, args, .call = .call)
185152
)
153+
186154
invisible(res)
187155
}
188156

@@ -337,3 +305,41 @@ quarto_binary_sitrep <- function(verbose = TRUE, debug = FALSE) {
337305

338306
return(same_config)
339307
}
308+
309+
310+
wrap_quarto_error <- function(cnd, args, .call = rlang::caller_env()) {
311+
if (!inherits(cnd, "system_command_status_error")) {
312+
cli::cli_abort(
313+
c("!" = "Error running quarto CLI from R."),
314+
call = .call,
315+
parent = cnd
316+
)
317+
} else {
318+
msg <- c(x = "Error returned by quarto CLI.")
319+
# if there is an error message from quarto CLI, add it to the message
320+
if (cnd$stderr != "") {
321+
quarto_error_msg <- xfun::split_lines(cnd$stderr)
322+
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
323+
msg <- c(
324+
msg,
325+
" " = paste0(rep("-", nchar(msg)), collapse = ""),
326+
quarto_error_msg
327+
)
328+
}
329+
330+
# if `--quiet` has been set, quarto CLI won't report any error (cnd$stderr will be empty)
331+
# So remind user to run without `--quiet` to see the full error message
332+
if (cli_arg_quiet() %in% args) {
333+
msg <- c(
334+
msg,
335+
"i" = "Rerun with `quiet = FALSE` to see the full error message."
336+
)
337+
}
338+
339+
cli::cli_abort(
340+
msg,
341+
call = .call,
342+
parent = cnd,
343+
)
344+
}
345+
}

0 commit comments

Comments
 (0)