flog.try: Trys to execute an expression with errors logged.

View source: R/ErrorHandling.R

flog.tryR Documentation

Trys to execute an expression with errors logged.

Description

This is a version of try which logs errors using the flog.logger mechanism.

Usage

flog.try(expr, context = deparse(substitute(expr)), loggername = flog.namespace(), 
         tracelevel = c("WARN", "ERROR", "FATAL"))

Arguments

expr

An R expression to be executed.

context

A character string defining what was operation is being performed for use in the log message.

loggername

A package name defining the logger to be used. See flog.namespace.

tracelevel

A character vector. In response to signals of the listed types, a stack trace will be sent to the log file.

Details

This function behaves like the try function, attempt to execute expr. If successful, the result is returned, if not an object of class try-error is returned, so that the calling function can figure out how to proceed.

It has two important difference from try. The first is the context argument which provides information about what was happening when the error was generated. In a large problem, this can provide vital debugging information, like the issue was with a particular node in a graph.

The second is that the error message and the stack trace are posted to the logging stream using the flog.logger function. This makes the code easier to use in server processes.

Value

Either the result of running expr or an object of class try-error.

Note

I should move this to the RGAutils package as it is generally useful.

Author(s)

Russell Almond

See Also

try, flog.logger

The function maxAllTableParams shows an example of this in use.

Examples


## Not run: 
maxAllTableParams <- function (net, Mstepit=5,
                                    tol=sqrt(.Machine$double.eps),
                                    debug=FALSE) {
  Errs <- list()
  netnm <- PnetName(net)
  lapply(PnetPnodes(net),
         function (nd) {
           ndnm <- PnodeName(nd)
           flog.debug("Updating params for node 
           out <- flog.try(maxCPTParam(nd,Mstepit,tol),
                           context=sprintf("Updating params for node 
                                           ndnm, netnm))
           if (is(out,'try-error')) {
             Errs <- c(Errs,out)
             if (debug) recover()
           }
         })
  if (length(Errs) >0L)
    stop("Errors encountered while updating parameters for ",netnm)
  invisible(net)
}

## End(Not run)

ralmond/Peanut documentation built on Sept. 19, 2023, 8:27 a.m.