| 1 |
#' @details `extractStackTrace` takes a list of calls (e.g. as returned |
| 2 |
#' from `conditionStackTrace(cond)`) and returns a data frame with one |
| 3 |
#' row for each stack frame and the columns `num` (stack frame number), |
| 4 |
#' `call` (a function name or similar), and `loc` (source file path |
| 5 |
#' and line number, if available). It was deprecated after shiny 1.0.5 because |
| 6 |
#' it doesn't support deep stack traces. |
| 7 |
#' @rdname stacktrace |
| 8 |
#' @export |
| 9 |
extractStackTrace <- function(calls, |
| 10 |
full = get_devmode_option("shiny.fullstacktrace", FALSE), |
| 11 |
offset = getOption("shiny.stacktraceoffset", TRUE)) { |
| 12 |
|
| 13 |
srcrefs <- getSrcRefs(calls) |
| 14 |
if (offset) { |
| 15 |
# Offset calls vs. srcrefs by 1 to make them more intuitive. |
| 16 |
# E.g. for "foo [bar.R:10]", line 10 of bar.R will be part of |
| 17 |
# the definition of foo(). |
| 18 |
srcrefs <- c(utils::tail(srcrefs, -1), list(NULL)) |
| 19 |
} |
| 20 |
calls <- setSrcRefs(calls, srcrefs) |
| 21 |
|
| 22 |
callnames <- getCallNames(calls) |
| 23 |
|
| 24 |
# Hide and show parts of the callstack based on ..stacktrace(on|off).. |
| 25 |
if (full) { |
| 26 |
toShow <- rep.int(TRUE, length(calls)) |
| 27 |
} else { |
| 28 |
# Remove stop(), .handleSimpleError(), and h() calls from the end of |
| 29 |
# the calls--they don't add any helpful information. But only remove |
| 30 |
# the last *contiguous* block of them, and then, only if they are the |
| 31 |
# last thing in the calls list. |
| 32 |
hideable <- callnames %in% c("stop", ".handleSimpleError", "h") |
| 33 |
# What's the last that *didn't* match stop/.handleSimpleError/h? |
| 34 |
lastGoodCall <- max(which(!hideable)) |
| 35 |
toRemove <- length(calls) - lastGoodCall |
| 36 |
# But don't remove more than 5 levels--that's an indication we might |
| 37 |
# have gotten it wrong, I guess |
| 38 |
if (toRemove > 0 && toRemove < 5) { |
| 39 |
calls <- utils::head(calls, -toRemove) |
| 40 |
callnames <- utils::head(callnames, -toRemove) |
| 41 |
} |
| 42 |
|
| 43 |
toShow <- stripStackTraces(list(callnames))[[1]] |
| 44 |
|
| 45 |
toShow <- |
| 46 |
toShow & |
| 47 |
# doTryCatch, tryCatchOne, and tryCatchList are not informative--they're |
| 48 |
# just internals for tryCatch |
| 49 |
!(callnames %in% c("doTryCatch", "tryCatchOne", "tryCatchList")) & |
| 50 |
# doWithOneRestart and withOneRestart are not informative--they're |
| 51 |
# just internals for withRestarts |
| 52 |
!(callnames %in% c("withOneRestart", "doWithOneRestart")) |
| 53 |
} |
| 54 |
calls <- calls[toShow] |
| 55 |
|
| 56 |
|
| 57 |
calls <- rev(calls) # Show in traceback() order |
| 58 |
index <- rev(which(toShow)) |
| 59 |
width <- floor(log10(max(index))) + 1 |
| 60 |
|
| 61 |
data.frame( |
| 62 |
num = index, |
| 63 |
call = getCallNames(calls), |
| 64 |
loc = getLocs(calls), |
| 65 |
# category = getCallCategories(calls), |
| 66 |
stringsAsFactors = FALSE |
| 67 |
) |
| 68 |
} |
| 69 |
|
| 70 |
cleanLocs <- function(locs) { |
| 71 |
locs[!grepl("test-stacks\\.R", locs, perl = TRUE)] <- "" |
| 72 |
# sub("^.*#", "", locs) |
| 73 |
locs |
| 74 |
} |
| 75 |
|
| 76 |
dumpTests <- function(df) { |
| 77 |
print(bquote({ |
| 78 |
expect_equal(df$num, .(df$num)) |
| 79 |
expect_equal(df$call, .(df$call)) |
| 80 |
expect_equal(nzchar(df$loc), .(nzchar(df$loc))) |
| 81 |
})) |
| 82 |
} |
| 83 |
|
| 84 |
# Helper: run a render function whose body throws an error, capture the |
| 85 |
# stack trace, apply fence-based filtering, and return the filtered data |
| 86 |
# frame. The render function body should call a function that calls stop(). |
| 87 |
# `needs_session` indicates whether the render function requires |
| 88 |
# shinysession/name parameters (TRUE for markRenderFunction-based renders |
| 89 |
# like renderPlot and renderPrint, FALSE for createRenderFunction-based |
| 90 |
# renders like renderText/renderTable/renderUI/renderImage which can be |
| 91 |
# called with no args). |
| 92 |
captureFilteredRenderTrace <- function(render_fn, needs_session = TRUE) { |
| 93 |
session <- MockShinySession$new() |
| 94 |
on.exit(if (!session$isClosed()) session$close()) |
| 95 |
|
| 96 |
res <- try({ |
| 97 |
captureStackTraces({ |
| 98 |
isolate({ |
| 99 |
withReactiveDomain(session, { |
| 100 |
if (needs_session) { |
| 101 |
render_fn(shinysession = session, name = "testoutput") |
| 102 |
} else { |
| 103 |
render_fn() |
| 104 |
} |
| 105 |
}) |
| 106 |
}) |
| 107 |
}) |
| 108 |
}, |
| 109 |
silent = TRUE) |
| 110 |
|
| 111 |
cond <- attr(res, "condition", exact = TRUE) |
| 112 |
stopifnot(!is.null(cond)) |
| 113 |
stopifnot(!is.null(conditionStackTrace(cond))) |
| 114 |
|
| 115 |
suppressMessages( |
| 116 |
extractStackTrace(conditionStackTrace(cond), full = FALSE) |
| 117 |
) |
| 118 |
} |