| 1 |
formatError <- function(err, full = FALSE, offset = TRUE, cleanPaths = TRUE) { |
| 2 |
# This complicated capturing code is necessary because printStackTrace uses a |
| 3 |
# combination of `message()` and `cat(file=stderr())` to print the error, |
| 4 |
# stack traces, and stack trace boundaries ("From earlier call:"). We want to |
| 5 |
# treat all of it as part of the same string. |
| 6 |
|
| 7 |
str <- noquote(capture.output( |
| 8 |
suppressWarnings( |
| 9 |
suppressMessages( |
| 10 |
withCallingHandlers( |
| 11 |
{ |
| 12 |
printError(err, full = full, offset = offset) |
| 13 |
}, |
| 14 |
warning = function(cnd) { |
| 15 |
cat(conditionMessage(cnd), "\n", sep = "", file = stderr()) |
| 16 |
}, |
| 17 |
message = function(cnd) { |
| 18 |
cat(conditionMessage(cnd), file = stderr()) |
| 19 |
} |
| 20 |
) |
| 21 |
) |
| 22 |
), |
| 23 |
type = "message" |
| 24 |
)) |
| 25 |
|
| 26 |
# Remove directories and line numbers from file/line references, e.g. |
| 27 |
# 53: callback [/Users/jcheng/Development/rstudio/shiny/R/conditions.R#155] |
| 28 |
# becomes |
| 29 |
# 53: callback [conditions.R#XXX] |
| 30 |
# |
| 31 |
# This is to make the snapshot tests more stable across different machines and |
| 32 |
# ignores benign code movement within a file. |
| 33 |
str <- sub("#\\d+\\]$", "#XXX]", str, perl = TRUE) |
| 34 |
# Remove any file/line number reference that's not test-stacks-deep.R. These |
| 35 |
# are just too inconsistent across different ways of invoking testthat--not |
| 36 |
# relative vs. absolute paths, but whether the file/line number is included at |
| 37 |
# all! |
| 38 |
str <- sub(" \\[(?!test-stacks-deep.R)[^[]+#XXX\\]", "", str, perl = TRUE) |
| 39 |
# The frame numbers vary too much between different ways of invoking testthat |
| 40 |
# ("Run Tests" editor toolbar button and "Test" Build tab button in RStudio, |
| 41 |
# devtools::test(), etc.) so we blank them out. |
| 42 |
str <- sub("^[ \\d]+:", " :", str, perl = TRUE) |
| 43 |
str |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
describe("deep stack trace filtering", { |
| 48 |
it("passes smoke test", { |
| 49 |
st <- list( |
| 50 |
c( |
| 51 |
common <- c("1", "2", "..stacktraceoff..", "3", "..stacktracefloor.."), |
| 52 |
"4", "..stacktraceon..", "5" |
| 53 |
), |
| 54 |
c(common, "6", "..stacktraceoff..", "7"), |
| 55 |
c(common, "8", "..stacktraceon.."), |
| 56 |
c(common, "9") |
| 57 |
) |
| 58 |
|
| 59 |
expect_equal( |
| 60 |
stripStackTraces(values = TRUE, st), |
| 61 |
jsonlite::fromJSON('[["1", "2", "5"],["6"],[],["9"]]') |
| 62 |
) |
| 63 |
}) |
| 64 |
|
| 65 |
it("handles null cases", { |
| 66 |
expect_equal( |
| 67 |
stripStackTraces(values = TRUE, list(c())), |
| 68 |
list(character(0)) |
| 69 |
) |
| 70 |
}) |
| 71 |
|
| 72 |
it("handles various edge cases", { |
| 73 |
expect_equal( |
| 74 |
stripStackTraces(values = TRUE, list( |
| 75 |
c("..stacktraceoff..", "..stacktraceoff..") |
| 76 |
)), |
| 77 |
list(character(0)) |
| 78 |
) |
| 79 |
|
| 80 |
expect_equal( |
| 81 |
stripStackTraces(values = TRUE, list( |
| 82 |
c("..stacktraceoff..", "..stacktraceoff.."), |
| 83 |
c(), |
| 84 |
c("..stacktraceon.."), |
| 85 |
c("..stacktraceon.."), |
| 86 |
c("1") |
| 87 |
)), |
| 88 |
list(character(0), character(0), character(0), character(0), "1") |
| 89 |
) |
| 90 |
}) |
| 91 |
}) |
| 92 |
|
| 93 |
test_that("deep stack capturing", { |
| 94 |
# base::tryCatch internals changed in 4.5.2 |
| 95 |
skip_unless_r(">= 4.5.2") |
| 96 |
skip_if_not_installed("testthat", "3.3.0") |
| 97 |
|
| 98 |
`%...>%` <- promises::`%...>%` |
| 99 |
`%...!%` <- promises::`%...!%` |
| 100 |
finally <- promises::finally |
| 101 |
|
| 102 |
err <- NULL |
| 103 |
captureStackTraces({ |
| 104 |
promise_resolve("one") %...>% { |
| 105 |
promise_reject("error") %...!% { |
| 106 |
finally(promise_resolve("two"), ~{ |
| 107 |
stop("boom") |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
}) %...!% (function(err) { |
| 112 |
err <<- err |
| 113 |
}) |
| 114 |
|
| 115 |
wait_for_it() |
| 116 |
|
| 117 |
expect_s3_class(err, "error", exact = FALSE) |
| 118 |
expect_snapshot(cat(sep="\n", formatError(err))) |
| 119 |
expect_snapshot(cat(sep="\n", formatError(err, full = TRUE))) |
| 120 |
}) |
| 121 |
|
| 122 |
test_that("deep stack capturing within reactives", { |
| 123 |
rerr <- NULL |
| 124 |
observe({ |
| 125 |
promise_resolve("one") %...>% { |
| 126 |
promise_resolve("two") %...>% { |
| 127 |
stop("boom") |
| 128 |
} |
| 129 |
} %...!% (function(err) { |
| 130 |
rerr <<- err |
| 131 |
}) |
| 132 |
}) |
| 133 |
|
| 134 |
flushReact() |
| 135 |
wait_for_it() |
| 136 |
|
| 137 |
expect_s3_class(rerr, "error", exact = FALSE) |
| 138 |
expect_length(attr(rerr, "deep.stack.trace"), 2) |
| 139 |
}) |
| 140 |
|
| 141 |
test_that("deep stacks long chain", { |
| 142 |
op <- options(shiny.deepstacktrace = 3L) |
| 143 |
on.exit(options(op), add = TRUE, after = FALSE) |
| 144 |
|
| 145 |
# Without deep stack traces, the stack trace would give no clue that the error |
| 146 |
# originally started from a call to `A__()`. With deep stack traces, we can |
| 147 |
# see that the error originated from `A__` and passed through `I__` and `J__`. |
| 148 |
# But due to culling, we don't see `B__` through `H__`--these are omitted for |
| 149 |
# brevity and to prevent unbounded growth of the accounting we do. |
| 150 |
|
| 151 |
A__ <- function() promise_resolve(TRUE) %...>% B__() |
| 152 |
B__ <- function(x) promise_resolve(TRUE) %...>% C__() |
| 153 |
C__ <- function(x) promise_resolve(TRUE) %...>% D__() |
| 154 |
D__ <- function(x) promise_resolve(TRUE) %...>% E__() |
| 155 |
E__ <- function(x) promise_resolve(TRUE) %...>% F__() |
| 156 |
F__ <- function(x) promise_resolve(TRUE) %...>% G__() |
| 157 |
G__ <- function(x) promise_resolve(TRUE) %...>% H__() |
| 158 |
H__ <- function(x) promise_resolve(TRUE) %...>% I__() |
| 159 |
I__ <- function(x) promise_resolve(TRUE) %...>% J__() |
| 160 |
J__ <- function(x) promise_resolve(TRUE) %...>% { stop("boom") } |
| 161 |
|
| 162 |
dserr <- NULL |
| 163 |
captureStackTraces( |
| 164 |
A__() |
| 165 |
) %...!% (function(err) { |
| 166 |
dserr <<- err |
| 167 |
}) |
| 168 |
|
| 169 |
wait_for_it() |
| 170 |
|
| 171 |
expect_s3_class(dserr, "error", exact = FALSE) |
| 172 |
expect_snapshot(cat(sep="\n", stacktrace <- formatError(dserr))) |
| 173 |
# Ensure we dropTrivialTestFrames only when snapshotting |
| 174 |
expect_false(length(stacktrace) == length(formatError(dserr))) |
| 175 |
# Ensure that A__ through J__ are present in the traces |
| 176 |
for (letter in LETTERS[1:10]) { |
| 177 |
expect_length(which(grepl(paste0(letter, "__"), stacktrace)), 1L) |
| 178 |
} |
| 179 |
}) |
| 180 |
|
| 181 |
test_that("Deep stack deduplication", { |
| 182 |
recursive_promise <- function(n) { |
| 183 |
if (n <= 0) { |
| 184 |
stop("boom") |
| 185 |
} |
| 186 |
|
| 187 |
p <- promises::promise_resolve(TRUE) |
| 188 |
promises::then(p, ~{ |
| 189 |
recursive_promise(n - 1) |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
op <- options(shiny.deepstacktrace = TRUE) |
| 194 |
on.exit(options(op), add = TRUE, after = FALSE) |
| 195 |
|
| 196 |
uerr <- NULL |
| 197 |
captureStackTraces(recursive_promise(100)) %...!% (function(err) { |
| 198 |
uerr <<- err |
| 199 |
}) |
| 200 |
|
| 201 |
wait_for_it() |
| 202 |
|
| 203 |
expect_s3_class(uerr, "error", exact = FALSE) |
| 204 |
# Even though we traveled through 100 promises recursively, we only retained |
| 205 |
# the unique ones |
| 206 |
expect_identical(length(attr(uerr, "deep.stack.trace", exact = TRUE)), 2L) |
| 207 |
}) |
| 208 |
|
| 209 |
test_that("stack trace stripping works", { |
| 210 |
A__ <- function() promise_resolve(TRUE) %...>% B__() |
| 211 |
B__ <- function(x) promise_resolve(TRUE) %...>% { ..stacktraceoff..(C__()) } |
| 212 |
C__ <- function(x) promise_resolve(TRUE) %...>% D__() |
| 213 |
D__ <- function(x) promise_resolve(TRUE) %...>% { ..stacktraceon..(E__()) } |
| 214 |
E__ <- function(x) promise_resolve(TRUE) %...>% { stop("boom") } |
| 215 |
|
| 216 |
strperr <- NULL |
| 217 |
captureStackTraces(A__()) %...!% (function(err) { |
| 218 |
strperr <<- err |
| 219 |
}) |
| 220 |
|
| 221 |
..stacktracefloor..( |
| 222 |
wait_for_it() |
| 223 |
) |
| 224 |
|
| 225 |
expect_s3_class(strperr, "error", exact = FALSE) |
| 226 |
|
| 227 |
str <- formatError(strperr) |
| 228 |
expect_length(which(grepl("A__", str)), 1L) |
| 229 |
expect_length(which(grepl("B__", str)), 1L) |
| 230 |
expect_length(which(grepl("C__", str)), 0L) |
| 231 |
expect_length(which(grepl("D__", str)), 0L) |
| 232 |
expect_length(which(grepl("E__", str)), 1L) |
| 233 |
|
| 234 |
str_full <- formatError(strperr, full = TRUE) |
| 235 |
expect_length(which(grepl("A__", str_full)), 1L) |
| 236 |
expect_length(which(grepl("B__", str_full)), 1L) |
| 237 |
expect_length(which(grepl("C__", str_full)), 1L) |
| 238 |
expect_length(which(grepl("D__", str_full)), 1L) |
| 239 |
expect_length(which(grepl("E__", str_full)), 1L) |
| 240 |
}) |
| 241 |
|
| 242 |
test_that("coro async generator deep stack count is low", { |
| 243 |
skip_if_not_installed("coro") |
| 244 |
gen <- coro::async_generator(function() { |
| 245 |
for (i in 1:50) { |
| 246 |
await(coro::async_sleep(0.001)) |
| 247 |
yield(i) |
| 248 |
} |
| 249 |
stop("boom") |
| 250 |
}) |
| 251 |
|
| 252 |
cgerr <- NULL |
| 253 |
captureStackTraces( |
| 254 |
coro::async_collect(gen()) %...!% (function(err) { |
| 255 |
cgerr <<- err |
| 256 |
}) |
| 257 |
) |
| 258 |
|
| 259 |
wait_for_it() |
| 260 |
|
| 261 |
expect_s3_class(cgerr, "error", exact = FALSE) |
| 262 |
expect_length(attr(cgerr, "deep.stack.trace"), 2L) |
| 263 |
}) |