R/tcmsg.R

Defines functions tcmsg

Documented in tcmsg

#' tryCatch with Message
#'
#' Easy Try/Catch implementation to return the same message on error or warning. Makes it easier to write tryCatches.
#' Author: Bryce Chamberlain. Tech review: Lindsay Smelzter.
#'
#' @param code_block Code to run in Try Catch.
#' @param ... Strings to concatenate to form the message that is returned.
#'
#' @export
#'
#' @examples
#' tryCatch({ 
#'    tcmsg({ NULL = 1 }, 'Cannot assign to NULL','variable' ) 
#'  }, 
#'  error = function(e) print( e ) 
#'  )
#'
#' tryCatch({ 
#'    tcmsg({ as.numeric('abc') },'Issue in as.numeric()') 
#'   }, 
#'   warning = function(e) print( e ) 
#' )
tcmsg <- function( code_block, ... ){
  message = paste( ... , collapse = ' ' )
  tryCatch(
    code_block,
    error = function(e) stop( e, message ),
    warning = function(w) warning( w, message )
)}

Try the easyr package in your browser

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

easyr documentation built on March 31, 2023, 6:22 p.m.