Skip to content

Instantly share code, notes, and snippets.

@pteetor
Created July 1, 2022 11:53
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 pteetor/24201b1fa9a33f174121023541fe7506 to your computer and use it in GitHub Desktop.
Save pteetor/24201b1fa9a33f174121023541fe7506 to your computer and use it in GitHub Desktop.
R functions for reporting fatal errors
#' @export
fatal = function(..., sep = " ", caller = NULL) {
caller <- caller %||% as.list(sys.call(-1))[[1]]
msg <- paste0("[", caller, "] ", paste(..., sep = sep))
stop(msg, call. = FALSE)
}
#' @export
fatalIf = function(cond, ..., caller = NULL) {
if (cond) {
fatal(...,
caller = caller %||% as.list(sys.call(-1))[[1]] )
}
}
#' @export
fatalIfNot = function(cond, ..., caller = NULL) {
if (!cond) {
fatal(...,
caller = caller %||% as.list(sys.call(-1))[[1]] )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment