R/conditions.R

Defines functions sanitize_call with_handlers condition_handlers

condition_handlers <- function(watcher, on_error, on_warning, on_message) {
  list(
    message = function(cnd) {
      watcher$capture_plot_and_output()

      if (on_message$capture) {
        watcher$push(cnd)
      }
      if (on_message$silence) {
        invokeRestart("muffleMessage")
      }
    },
    warning = function(cnd) {
      # do not handle warnings that shortly become errors or have been silenced
      if (getOption("warn") >= 2 || getOption("warn") < 0) {
        return()
      }

      watcher$capture_plot_and_output()
      if (on_warning$capture) {
        cnd <- sanitize_call(cnd)
        watcher$push(cnd)
      }
      if (on_warning$silence) {
        invokeRestart("muffleWarning")
      }
    },
    error = function(cnd) {
      watcher$capture_plot_and_output()

      cnd <- sanitize_call(cnd)
      watcher$push(cnd)

      switch(on_error,
        continue = invokeRestart("eval_continue"),
        stop = invokeRestart("eval_stop"),
        error = invokeRestart("eval_error", cnd)
      )
    }
  )
}


with_handlers <- function(code, handlers) {
  if (!is.list(handlers)) {
    stop("`handlers` must be a list", call. = FALSE)
  }

  call <- as.call(c(quote(withCallingHandlers), quote(code), handlers))
  eval(call)
}

sanitize_call <- function(cnd) {
  if (identical(cnd$call, quote(eval(expr, envir)))) {
    cnd$call <- NULL
  }

  cnd
}

Try the evaluate package in your browser

Any scripts or data that you put into this service are public.

evaluate documentation built on Oct. 10, 2024, 5:06 p.m.