Skip to content

Instantly share code, notes, and snippets.

@goldingn
Created December 18, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldingn/20a39736b9cc28a5ad1fc9e98736db8b to your computer and use it in GitHub Desktop.
Save goldingn/20a39736b9cc28a5ad1fc9e98736db8b to your computer and use it in GitHub Desktop.
# lookup table of error messages (coud be read in from a file in the package)
lookup <- cbind(from = "there is no package called ‘pineapples’",
to = "no pineapples here!")
# swap over the message if there's a better one in the lookup
swap_message <- function (message) {
idx <- match(message, lookup[, "from"])
if (length(idx == 1) && !is.na(idx))
message <- lookup[idx, "to"]
message
}
# add message swapping to the base stop() function
unlockBinding("stop", baseenv() )
assign("stop",
local({
function (..., call. = TRUE, domain = NULL) {
args <- list(...)
if (length(args) == 1L && inherits(args[[1L]], "condition")) {
cond <- args[[1L]]
if (nargs() > 1L)
warning("additional arguments ignored in stop()")
message <- conditionMessage(cond)
message <- swap_message(message)
call <- conditionCall(cond)
.Internal(.signalCondition(cond, message, call))
.Internal(.dfltStop(message, call))
} else {
message <- .makeMessage(..., domain = domain)
message <- swap_message(message)
.Internal(stop (call., message))
}
}
}),
pos = baseenv()
)
# # test run:
# library (pineapples)
# > Error in library(pineapples) : no pineapples here!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment