R/bash.R

Defines functions callback_w callback_e bash

Documented in bash

#' Take a Bash
#' 
#' Equivalent to [tryCatch()].
#' 
#' @param expr Expression to run, passed to [tryCatch()].
#' @param e,w An object of class `Error` or `Warning` as 
#' returned by [e()] or [w()].
#' 
#' @examples 
#' safe_log <- function(x){
#'  result <- bash(log(x))
#'  
#'  if(is.e(result))
#'    stop(result$stop())
#' 
#'  return(result)
#' } 
#' 
#' if(interactive())
#'  safe_log("a")
#' 
#' @export 
bash <- function(expr, e = NULL, w = NULL){
  tryCatch(
    expr,
    error = callback_e(e),
    warning = callback_w(w)
  )
}

#' Create Callbacks for bash
#' 
#' Creates callback for [bash()].
#' 
#' @name callbacks
#' 
#' @noRd 
#' @keywords internal
callback_e <- function(e = NULL){
  return(
    function(err){
      if(is.null(e))
        return(e(err))

      if(is.function(e))
        return(e(err))
      
      return(e)
    }
  )
}

#' @rdname callbacks
#' @noRd 
#' @keywords internal
callback_w <- function(w = NULL){
  return(
    function(war){
      if(is.null(w))
        return(w(war))

      if(is.function(w))
        return(w(war))
      
      return(w)
    }
  )
}

Try the erratum package in your browser

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

erratum documentation built on Jan. 4, 2022, 1:08 a.m.