| 1 |
# utils ------------------------------------------------------------------- |
| 2 |
|
| 3 |
#' Demote an exception to a warning |
| 4 |
#' |
| 5 |
#' Throws a timestamped warning instead of stopping the program. This is |
| 6 |
#' the default exception handler used by [Loggers]. |
| 7 |
#' |
| 8 |
#' @param e an `error condition` object |
| 9 |
#' |
| 10 |
#' @return The warning as `character` vector |
| 11 |
#' @export |
| 12 |
#' |
| 13 |
#' @examples |
| 14 |
#' tryCatch(stop("an error has occurred"), error = default_exception_handler) |
| 15 |
#' |
| 16 |
default_exception_handler <- function(e){ |
| 17 |
ts <- format(Sys.time(), format = "%Y-%m-%d %H:%M:%OS3") |
| 18 |
call <- paste(trimws(format(e$call)), collapse = " ") |
| 19 |
logger <- format(e$logger$name) |
| 20 |
|
| 21 |
if ("appender" %in% names(e)){ |
| 22 |
appender <- paste0(class_fmt(e$appender, ignore = c("Filterable", "R6", "Appender"))) |
| 23 |
message <- sprintf("[%s] %s %s ~ error in `%s`: %s", ts, logger, appender, call, e$message) |
| 24 |
res <- AppenderWarning(message = message, error = e) |
| 25 |
|
| 26 |
} else { |
| 27 |
message <- sprintf("[%s] %s ~ error in `%s`: %s", ts, logger, call, e$message) |
| 28 |
res <- LoggerWarning(message = message, error = e) |
| 29 |
} |
| 30 |
warning(res) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
AppenderWarning <- function(message, class = NULL, call = NULL, ...){ |
| 36 |
LoggerWarning( |
| 37 |
message = message, |
| 38 |
class = union(class, "AppenderWarning"), |
| 39 |
call = call, |
| 40 |
... |
| 41 |
) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
LoggerWarning <- function(message, class = NULL, call = NULL, ...){ |
| 47 |
warning_condition( |
| 48 |
message = message, |
| 49 |
class = union(class, "LoggerWarning"), |
| 50 |
call = call, |
| 51 |
... |
| 52 |
) |
| 53 |
} |