Skip to content

Commit

Permalink
Stream messages to stdout by default (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Nov 21, 2023
1 parent 5f45d1d commit fc13102
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion crates/ark/src/modules/public/errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

# Inject our global error handler at the end.
# This allows other existing error handlers to run ahead of us.
handlers <- c(handlers, list(error = .ps.errors.globalErrorHandler))
handlers <- c(
handlers,
list(
error = .ps.errors.globalErrorHandler,
message = .ps.errors.globalMessageHandler
)
)
do.call(globalCallingHandlers, handlers)

# Tell rlang and base R not to print the error message, we will do it!
Expand Down Expand Up @@ -49,6 +55,22 @@
handle_error_rlang(cnd)
}

.ps.errors.globalMessageHandler <- function(cnd) {
# Decline to handle if we can't muffle the message (should only happen
# in extremely rare cases)
if (is.null(findRestart("muffleMessage"))) {
return()
}

# Output the condition message to the relevant stream (normally
# stdout). Note that for historical reasons, messages include a
# trailing newline
cat(conditionMessage(cnd), file = default_message_file())

# Silence default message handling
invokeRestart("muffleMessage")
}

.ps.errors.traceback <- function() {
traceback <- get0(".Traceback", baseenv(), ifnotfound = list())

Expand All @@ -59,3 +81,18 @@

format_traceback(traceback)
}

# If a sink is active (either on output or on messages) messages
# are always streamed to `stderr`. This follows rlang behaviour
# and ensures messages can be sinked from stderr consistently.
#
# Unlike rlang we don't make an exception for non-interactive sessions
# since Ark is meant to be run interactively.
default_message_file <- function() {
if (sink.number("output") == 0 &&
sink.number("message") == 2) {
stdout()
} else {
stderr()
}
}

0 comments on commit fc13102

Please sign in to comment.