R/DebugFn.R
DebugFnE.Rd
Run fn, save arguments, and environment on failure.
Please see: vignette("DebugFnW", package="wrapr")
.
DebugFnE(saveDest, fn, ...)
where to write captured state (determined by type): NULL random temp file, character temp file, name globalenv() variable, and function triggers callback.
function to call
arguments for fn
fn(...) normally, but if fn(...) throws an exception save to saveDest RDS of list r such that do.call(r$fn,r$args) repeats the call to fn with args.
dump.frames
, DebugFn
, DebugFnW
, DebugFnWE
, DebugPrintFn
, DebugFnE
, DebugPrintFnE
saveDest <- paste0(tempfile('debug'),'.RDS')
f <- function(i) { (1:10)[[i]] }
# correct run
DebugFnE(saveDest, f, 5)
#> [1] 5
# now re-run
# capture error on incorrect run
tryCatch(
DebugFnE(saveDest, f, 12),
error = function(e) { print(e) })
#> <simpleError in value[[3L]](cond): wrapr::DebugFnE: wrote '/var/folders/7f/sdjycp_d08n8wwytsbgwqgsw0000gn/T//RtmpZ4IuL6/debug11ef47bbe0bf0.RDS' on catching 'Error in (1:10)[[i]]: subscript out of bounds'
#> You can reproduce the error with:
#> 'p <- readRDS('/var/folders/7f/sdjycp_d08n8wwytsbgwqgsw0000gn/T//RtmpZ4IuL6/debug11ef47bbe0bf0.RDS'); do.call(p$fn, p$args, envir= p$env)'>
# examine details
situation <- readRDS(saveDest)
str(situation)
#> List of 4
#> $ fn :function (i)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 3 6 3 32 6 32 3 3
#> .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fba0a892ac8>
#> $ args :List of 1
#> ..$ : num 12
#> $ env :<environment: 0x7fba0a892748>
#> $ fn_name: chr "f"
# fix and re-run
situation$args[[1]] <- 6
do.call(situation$fn, situation$args, envir=situation$env)
#> [1] 6
# clean up
file.remove(saveDest)
#> [1] TRUE