From 25c3ceb79c9a9c6e28985cbbacdebf4b56ccaf61 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 17 Apr 2023 16:08:28 -0500 Subject: [PATCH] when the connection is no longer valid, mention the most likely reason: closeAllConnections() might have been called https://github.com/quarto-dev/quarto-cli/issues/5214 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/watcher.R | 11 ++++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e6ac61b..1f6b787 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: evaluate Type: Package Title: Parsing and Evaluation Tools that Provide More Details than the Default -Version: 0.20.1 +Version: 0.20.2 Authors@R: c( person("Hadley", "Wickham", role = "aut"), person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), diff --git a/NEWS.md b/NEWS.md index eb53741..661b33e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ Version 0.21 these cause code and warnings (respectively) to be immediately emitted to `stderr()`. This is useful for logging in unattended environments (#118). +- Improved the error message when users accidentally called `closeAllConnections()` (thanks, @guslipkin, quarto-dev/quarto-cli#5214). + Version 0.20 ================================================================================ diff --git a/R/watcher.R b/R/watcher.R index a5a2d87..7f6b334 100644 --- a/R/watcher.R +++ b/R/watcher.R @@ -15,7 +15,7 @@ watchout <- function(debug = FALSE) { list( get_new = function(plot = FALSE, incomplete_plots = FALSE, text_callback = identity, graphics_callback = identity) { - incomplete <- isIncomplete(con) + incomplete <- test_con(con, isIncomplete) if (incomplete) cat("\n") out <- list() @@ -42,8 +42,7 @@ watchout <- function(debug = FALSE) { pause = function() sink(), unpause = function() sink(con, split = debug), close = function() { - if (!isOpen(con)) - stop("something bad happened... did you use closeAllConnections()?") + test_con(con, isOpen) sink() close(con) output @@ -52,6 +51,12 @@ watchout <- function(debug = FALSE) { ) } +test_con = function(con, test) { + tryCatch(test(con), error = function(e) stop( + e$message, '... Please make sure not to call closeAllConnections().' + )) +} + .env = new.env() .env$flush_console = function() {}