Skip to content

Improve quarto_run error handling #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions R/quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,18 @@ quarto_run <- function(
...,
.call = rlang::caller_env()
) {
res <- tryCatch(
{
processx::run(
quarto_bin,
args = args,
echo = echo,
error_on_status = TRUE,
echo_cmd = echo_cmd,
...
)
},
error = function(e) {
msg <- c(x = "Error running quarto cli.")
# if there is an error message from quarto CLI, add it to the message
if (e$stderr != "") {
quarto_error_msg <- xfun::split_lines(e$stderr)
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
msg <- c(
msg,
" " = paste0(rep("-", nchar(msg)), collapse = ""),
quarto_error_msg
)
}

# if `--quiet` has been set, quarto CLI won't report any error (e$stderr will be empty)
# So remind user to run without `--quiet` to see the full error message
if (cli_arg_quiet() %in% args)
msg <- c(
msg,
"i" = "Rerun with `quiet = FALSE` to see the full error message."
)

cli::cli_abort(msg, call = .call, parent = e)
}
res <- withCallingHandlers(
processx::run(
quarto_bin,
args = args,
echo = echo,
error_on_status = TRUE,
echo_cmd = echo_cmd,
...
),
error = function(cnd) wrap_quarto_error(cnd, args, .call = .call)
)

invisible(res)
}

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

return(same_config)
}


wrap_quarto_error <- function(cnd, args, .call = rlang::caller_env()) {
msg <- c(x = "Error running quarto cli.")
# if there is an error message from quarto CLI, add it to the message
if (cnd$stderr != "") {
quarto_error_msg <- xfun::split_lines(cnd$stderr)
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
msg <- c(
msg,
" " = paste0(rep("-", nchar(msg)), collapse = ""),
quarto_error_msg
)
}

# if `--quiet` has been set, quarto CLI won't report any error (cnd$stderr will be empty)
# So remind user to run without `--quiet` to see the full error message
if (cli_arg_quiet() %in% args)
msg <- c(
msg,
"i" = "Rerun with `quiet = FALSE` to see the full error message."
)

cli::cli_abort(
msg,
call = .call,
parent = cnd,
)
}