Learn R Programming

tryCatchLog (version 1.0.2)

tryCatchLog: Try an expression with condition logging and error handling

Description

This function evaluates an expression passed in the expr parameter and executes the error handler function passed as parameter error in case of an error condition.

Usage

tryCatchLog(expr, ..., finally = NULL,
  write.error.dump.file = getOption("tryCatchLog.write.error.dump.file",
  FALSE), silent.warnings = getOption("tryCatchLog.silent.warnings", FALSE),
  silent.messages = getOption("tryCatchLog.silent.messages", FALSE))

Arguments

expr

expression to be evaluated

...

condition handler functions (as in tryCatch). Usual condition names are error, warning, message and interrupt. All condition handlers are passed to tryCatch as is (no filtering, wrapping or changing of semantics).

finally

expression to be evaluated at the end

write.error.dump.file

TRUE: Saves a dump of the workspace and the call stack named dump_<YYYYMMDD_HHMMSS>.rda

silent.warnings

TRUE: Warnings are logged, but not propagated to the caller. FALSE: Warnings are logged and treated according to the global setting in getOption("warn"). See also warning.

silent.messages

TRUE: Messages are logged, but not propagated to the caller. FALSE: Messages are logged and propagated to the caller.

Value

the value of the expression passed in as parameter "expr"

Best practices

To avoid that too many dump files filling your disk space you should omit the write.error.dump.file parameter and instead set its default value using the option tryCatchLog.write.error.dump.file in your .Rprofile file instead (or in a startup R script that sources your actual script). In case of an error (that you can reproduce) you set the option to TRUE and re-run your script. Then you are able to examine the program state that led to the error by debugging the saved dump file.

To see the source code references (source file names and line numbers) in the stack traces you must set this option before executing your code: options(keep.source = TRUE)

You can execute your code as batch with Rscript using this shell script command: Rscript -e "options(keep.source = TRUE); source('my_main_function.R')"

Details

The finally expression is then always evaluated at the end.

Conditions are logged with the function call stack (including file names and line numbers).

This function shall overcome some drawbacks of the standard tryCatch function. For more details see https://github.com/aryoda/tryCatchLog.

Before you call tryCatchLog for the first time you should initialize the futile.logger first:

  library(futile.logger)
  flog.appender(appender.file("my_app.log"))
  flog.threshold(INFO)    # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

If you don't initialize the futile.logger at all the logging information will be written on the console only.

The following conditions are logged using the futile.logger package:

  1. error -> flog.error

  2. warning -> flog.warn

  3. message -> flog.info

`tryCatchLog` does only catch the above conditions, other (user-defined) conditions are currently not catched and therefore not logged.

The log contains the call stack with the file names and line numbers (if available).

R does track source code references only if you set the option keep.source to TRUE via options(keep.source = TRUE). Without this option this function cannot enrich source code references. If you use Rscript to start a non-interactive R script as batch job you have to set this option since it is FALSE by default. You can add this option to your .Rprofile file or use a startup R script that sets this option and sources your actual R script then.

By default, most packages are built without source reference information. Setting the environment variable R_KEEP_PKG_SOURCE=yes before installing a source package will tell R to keep the source references. You can also use options(keep.source.pkgs = TRUE) before you install a package.

Setting the parameter tryCatchLog.write.error.dump.file to TRUE allows a post-mortem analysis of the program state that led to the error. The dump contains the workspace and in the variable "last.dump" the call stack (sys.frames). This feature is very helpful for non-interactive R scripts ("batches").

To start a post-mortem analysis after an error open a new R session and enter: load("dump_20161016_164050.rda") # replace the dump file name with your real file name debugger(last.dump)

Note that the dump does not contain the loaded packages when the dump file was created and a dump loaded into memory does therefore not use exactly the same search path. This means:

  1. the program state is not exactly reproducible if objects are stored within a package namespace

  2. you cannot step through your source code in a reproducible way after loading the image if your source code calls functions of non-default packages

References

https://stackoverflow.com/questions/39964040/r-catch-errors-and-continue-execution-after-logging-the-stacktrace-no-tracebac

See Also

tryLog, limitedLabels, get.pretty.call.stack, getOption, last.tryCatchLog.result

Examples

Run this code
# NOT RUN {
tryCatchLog(log(-1))   # logs a warning
# }

Run the code above in your browser using DataLab