View source: R/ErrorHandling.R
flog.try | R Documentation |
This is a version of try
which logs errors using
the flog.logger
mechanism.
flog.try(expr, context = deparse(substitute(expr)), loggername = flog.namespace(),
tracelevel = c("WARN", "ERROR", "FATAL"))
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
|
tracelevel |
A character vector. In response to signals of the listed types, a stack trace will be sent to the log file. |
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.
Either the result of running expr
or an object of class
try-error
.
I should move this to the RGAutils package as it is generally useful.
Russell Almond
try
, flog.logger
The function maxAllTableParams
shows an example of this
in use.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.