| 1 |
causeError <- function(full) { |
| 2 |
A <- function() { |
| 3 |
stop("foo") |
| 4 |
} |
| 5 |
|
| 6 |
B <- function() { |
| 7 |
A() |
| 8 |
} |
| 9 |
|
| 10 |
C <- reactive({ |
| 11 |
B() |
| 12 |
}) |
| 13 |
|
| 14 |
res <- try({ |
| 15 |
captureStackTraces({ |
| 16 |
isolate({ |
| 17 |
renderTable({ |
| 18 |
C() |
| 19 |
}, server = FALSE)() |
| 20 |
}) |
| 21 |
}) |
| 22 |
}, |
| 23 |
silent = TRUE) |
| 24 |
cond <- attr(res, "condition", exact = TRUE) |
| 25 |
|
| 26 |
suppressMessages(df <- extractStackTrace(conditionStackTrace(cond), full = full)) |
| 27 |
df$loc <- cleanLocs(df$loc) |
| 28 |
# Compensate for this test being called from different call sites; |
| 29 |
# whack the top n frames off using the `num` frame column |
| 30 |
df <- df[df$num >= sys.nframe(), ] |
| 31 |
df$num <- df$num - sys.nframe() |
| 32 |
df |
| 33 |
} |
| 34 |
|
| 35 |
test_that("integration tests", { |
| 36 |
if (shiny_otel_tracer()$is_enabled()) { |
| 37 |
announce_snapshot_file(name = "stacks.md") |
| 38 |
|
| 39 |
skip("Skipping stack trace tests when OpenTelemetry is already enabled") |
| 40 |
} |
| 41 |
|
| 42 |
# The expected call stack can be changed by other packages (namely, promises). |
| 43 |
# If promises changes its internals, it can break this test on CRAN. Because |
| 44 |
# CRAN package releases are generally not synchronized (that is, promises and |
| 45 |
# shiny can't be updated at the same time, unless there is manual intervention |
| 46 |
# from CRAN maintaineres), these specific test expectations make it impossible |
| 47 |
# to release a version of promises that will not break this test and cause |
| 48 |
# problems on CRAN. |
| 49 |
skip_on_cran() |
| 50 |
|
| 51 |
df_integration_slim <- causeError(full = FALSE) |
| 52 |
# dumpTests(df_integration_slim) |
| 53 |
|
| 54 |
expect_snapshot(df_integration_slim) |
| 55 |
|
| 56 |
df_integration_full <- causeError(full = TRUE) |
| 57 |
|
| 58 |
expect_snapshot(df_integration_full) |
| 59 |
# dumpTests(df_integration_full) |
| 60 |
}) |
| 61 |
|
| 62 |
test_that("shiny.error", { |
| 63 |
caught <- NULL |
| 64 |
op <- options(shiny.error = function() { caught <<- TRUE }) |
| 65 |
on.exit(options(op)) |
| 66 |
|
| 67 |
# Regular errors should be intercepted by shiny.error |
| 68 |
try(shiny:::shinyCallingHandlers(stop("boom")), silent = TRUE) |
| 69 |
expect_true(caught) |
| 70 |
|
| 71 |
caught <- NULL |
| 72 |
|
| 73 |
# Validation errors shouldn't be intercepted by shiny.error |
| 74 |
|
| 75 |
try(shiny:::shinyCallingHandlers(validate(need(NULL, FALSE))), silent = TRUE) |
| 76 |
expect_null(caught) |
| 77 |
|
| 78 |
er <- eventReactive(NULL, { "Hello" }) |
| 79 |
try(shiny:::shinyCallingHandlers(isolate(er())), silent = TRUE) |
| 80 |
expect_null(caught) |
| 81 |
try(shiny:::shinyCallingHandlers(isolate(er())), silent = TRUE) |
| 82 |
expect_null(caught) |
| 83 |
}) |
| 84 |
|
| 85 |
test_that("chained silent errors aren't intercepted (tidyverse/dplyr#5552)", { |
| 86 |
withr::local_options( |
| 87 |
shiny.error = function() caught <<- TRUE |
| 88 |
) |
| 89 |
|
| 90 |
f <- function() { |
| 91 |
withCallingHandlers( |
| 92 |
validate(need(NULL, FALSE)), |
| 93 |
error = function(cnd) { |
| 94 |
rlang::abort("Child error.", parent = cnd) |
| 95 |
} |
| 96 |
) |
| 97 |
} |
| 98 |
caught <- NULL |
| 99 |
try(shiny:::shinyCallingHandlers(f()), silent = TRUE) |
| 100 |
expect_null(caught) |
| 101 |
|
| 102 |
caught <- NULL |
| 103 |
try(hybrid_chain(f()), silent = TRUE) |
| 104 |
expect_null(caught) |
| 105 |
}) |
| 106 |
|
| 107 |
test_that("validation error logging", { |
| 108 |
caught <- NULL |
| 109 |
|
| 110 |
# Given an error-throwing exception expr, execute it |
| 111 |
# using withLogErrors, and superassign the warning that |
| 112 |
# results (the error log is emitted using warning()) |
| 113 |
# into the parent variable `caught` |
| 114 |
captureErrorLog <- function(expr) { |
| 115 |
tryCatch( |
| 116 |
tryCatch( |
| 117 |
shiny::withLogErrors(expr), |
| 118 |
warning = function(cond) { |
| 119 |
caught <<- cond |
| 120 |
} |
| 121 |
), |
| 122 |
error = function(e) { |
| 123 |
} |
| 124 |
) |
| 125 |
} |
| 126 |
|
| 127 |
captureErrorLog(validate("boom")) |
| 128 |
expect_null(caught) |
| 129 |
|
| 130 |
caught <- NULL |
| 131 |
captureErrorLog(stop("boom")) |
| 132 |
expect_true(!is.null(caught)) |
| 133 |
}) |
| 134 |
|
| 135 |
test_that("observeEvent is not overly stripped (#4162)", { |
| 136 |
caught <- NULL |
| 137 |
..stacktraceoff..( |
| 138 |
..stacktracefloor..({ |
| 139 |
observeEvent(1, { |
| 140 |
tryCatch( |
| 141 |
captureStackTraces(stop("boom")), |
| 142 |
error = function(cond) { |
| 143 |
caught <<- cond |
| 144 |
} |
| 145 |
) |
| 146 |
}) |
| 147 |
flushReact() |
| 148 |
}) |
| 149 |
) |
| 150 |
st_str <- capture.output(printStackTrace(caught), type = "message") |
| 151 |
expect_match(st_str, "observeEvent\\(1\\)", all = FALSE) |
| 152 |
|
| 153 |
# Now same thing, but deep stack trace version |
| 154 |
|
| 155 |
A__ <- function() { |
| 156 |
promises::then(promises::promise_resolve(TRUE), ~{ |
| 157 |
stop("boom") |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
B__ <- function() { |
| 162 |
promises::then(promises::promise_resolve(TRUE), ~{ |
| 163 |
A__() |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
caught <- NULL |
| 168 |
..stacktraceoff..( |
| 169 |
..stacktracefloor..({ |
| 170 |
observeEvent(1, { |
| 171 |
captureStackTraces(promises::catch(B__(), ~{ |
| 172 |
caught <<- . |
| 173 |
})) |
| 174 |
}) |
| 175 |
flushReact() |
| 176 |
wait_for_it() |
| 177 |
}) |
| 178 |
) |
| 179 |
st_str <- capture.output(printStackTrace(caught), type = "message") |
| 180 |
# cat(st_str, sep = "\n") |
| 181 |
expect_match(st_str, "A__", all = FALSE) |
| 182 |
expect_match(st_str, "B__", all = FALSE) |
| 183 |
}) |
| 184 |
|
| 185 |
test_that("renderPlot stack trace fences hide internal rendering pipeline (#4357)", { |
| 186 |
skip_on_cran() |
| 187 |
|
| 188 |
skip_if_shiny_otel_tracer_is_enabled() |
| 189 |
|
| 190 |
userFunc <- function() { |
| 191 |
stop("test error in renderPlot") |
| 192 |
} |
| 193 |
|
| 194 |
df <- captureFilteredRenderTrace(renderPlot({ userFunc() })) |
| 195 |
|
| 196 |
expect_true("userFunc" %in% df$call) |
| 197 |
|
| 198 |
# Internal rendering pipeline frames should NOT appear in the filtered |
| 199 |
# stack trace. These are Shiny internals between the stack trace fences |
| 200 |
# that currently leak through due to missing fences. |
| 201 |
internal_render_frames <- c( |
| 202 |
"drawPlot", |
| 203 |
"drawReactive", |
| 204 |
"renderFunc", |
| 205 |
"startPNG" |
| 206 |
) |
| 207 |
|
| 208 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 209 |
expect_length(leaked, 0) |
| 210 |
}) |
| 211 |
|
| 212 |
test_that("renderPrint stack trace fences hide internal rendering pipeline (#4357)", { |
| 213 |
skip_on_cran() |
| 214 |
|
| 215 |
skip_if_shiny_otel_tracer_is_enabled() |
| 216 |
|
| 217 |
userFunc <- function() { |
| 218 |
stop("test error in renderPrint") |
| 219 |
} |
| 220 |
|
| 221 |
df <- captureFilteredRenderTrace(renderPrint({ userFunc() })) |
| 222 |
|
| 223 |
expect_true("userFunc" %in% df$call) |
| 224 |
|
| 225 |
internal_render_frames <- c("renderFunc") |
| 226 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 227 |
expect_length(leaked, 0) |
| 228 |
}) |
| 229 |
|
| 230 |
test_that("renderText stack trace fences hide internal rendering pipeline (#4357)", { |
| 231 |
skip_on_cran() |
| 232 |
|
| 233 |
skip_if_shiny_otel_tracer_is_enabled() |
| 234 |
|
| 235 |
userFunc <- function() { |
| 236 |
stop("test error in renderText") |
| 237 |
} |
| 238 |
|
| 239 |
df <- captureFilteredRenderTrace(renderText({ userFunc() }), needs_session = FALSE) |
| 240 |
|
| 241 |
expect_true("userFunc" %in% df$call) |
| 242 |
|
| 243 |
internal_render_frames <- c("renderFunc") |
| 244 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 245 |
expect_length(leaked, 0) |
| 246 |
}) |
| 247 |
|
| 248 |
test_that("renderUI stack trace fences hide internal rendering pipeline (#4357)", { |
| 249 |
skip_on_cran() |
| 250 |
|
| 251 |
skip_if_shiny_otel_tracer_is_enabled() |
| 252 |
|
| 253 |
userFunc <- function() { |
| 254 |
stop("test error in renderUI") |
| 255 |
} |
| 256 |
|
| 257 |
df <- captureFilteredRenderTrace(renderUI({ userFunc() }), needs_session = FALSE) |
| 258 |
|
| 259 |
expect_true("userFunc" %in% df$call) |
| 260 |
|
| 261 |
internal_render_frames <- c("renderFunc") |
| 262 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 263 |
expect_length(leaked, 0) |
| 264 |
}) |
| 265 |
|
| 266 |
test_that("renderTable stack trace fences hide internal rendering pipeline (#4357)", { |
| 267 |
skip_on_cran() |
| 268 |
|
| 269 |
skip_if_shiny_otel_tracer_is_enabled() |
| 270 |
|
| 271 |
userFunc <- function() { |
| 272 |
stop("test error in renderTable") |
| 273 |
} |
| 274 |
|
| 275 |
df <- captureFilteredRenderTrace( |
| 276 |
renderTable({ userFunc() }, server = FALSE), |
| 277 |
needs_session = FALSE |
| 278 |
) |
| 279 |
|
| 280 |
expect_true("userFunc" %in% df$call) |
| 281 |
|
| 282 |
internal_render_frames <- c("renderFunc") |
| 283 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 284 |
expect_length(leaked, 0) |
| 285 |
}) |
| 286 |
|
| 287 |
test_that("renderImage stack trace fences hide internal rendering pipeline (#4357)", { |
| 288 |
skip_on_cran() |
| 289 |
|
| 290 |
skip_if_shiny_otel_tracer_is_enabled() |
| 291 |
|
| 292 |
userFunc <- function() { |
| 293 |
stop("test error in renderImage") |
| 294 |
} |
| 295 |
|
| 296 |
df <- captureFilteredRenderTrace( |
| 297 |
renderImage({ userFunc() }, deleteFile = FALSE), |
| 298 |
needs_session = FALSE |
| 299 |
) |
| 300 |
|
| 301 |
expect_true("userFunc" %in% df$call) |
| 302 |
|
| 303 |
internal_render_frames <- c("renderFunc") |
| 304 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 305 |
expect_length(leaked, 0) |
| 306 |
}) |
| 307 |
|
| 308 |
test_that("legacyRenderDataTable stack trace fences hide internal rendering pipeline (#4357)", { |
| 309 |
skip_on_cran() |
| 310 |
|
| 311 |
skip_if_shiny_otel_tracer_is_enabled() |
| 312 |
|
| 313 |
userFunc <- function() { |
| 314 |
stop("test error in renderDataTable") |
| 315 |
} |
| 316 |
|
| 317 |
df <- captureFilteredRenderTrace( |
| 318 |
legacyRenderDataTable({ userFunc() }) |
| 319 |
) |
| 320 |
|
| 321 |
expect_true("userFunc" %in% df$call) |
| 322 |
|
| 323 |
internal_render_frames <- c("renderFunc") |
| 324 |
leaked <- df$call[df$call %in% internal_render_frames] |
| 325 |
expect_length(leaked, 0) |
| 326 |
}) |
| 327 |
|
| 328 |
test_that("markRenderFunction preserves user frames outside reactive domain", { |
| 329 |
skip_on_cran() |
| 330 |
|
| 331 |
skip_if_shiny_otel_tracer_is_enabled() |
| 332 |
|
| 333 |
# htmlwidgets-style: exprToFunction + markRenderFunction, no ..stacktraceon.. |
| 334 |
renderWidgetLike <- function(expr, env = parent.frame(), quoted = FALSE) { |
| 335 |
if (!quoted) expr <- substitute(expr) |
| 336 |
func <- exprToFunction(expr, env, TRUE) |
| 337 |
renderFunc <- function() { func() } |
| 338 |
markRenderFunction(textOutput, renderFunc) |
| 339 |
} |
| 340 |
|
| 341 |
userFunc <- function() stop("boom") |
| 342 |
render_fn <- renderWidgetLike({ userFunc() }) |
| 343 |
|
| 344 |
res <- try(captureStackTraces({ render_fn() }), silent = TRUE) |
| 345 |
cond <- attr(res, "condition", exact = TRUE) |
| 346 |
df <- extractStackTrace(conditionStackTrace(cond), full = FALSE) |
| 347 |
|
| 348 |
expect_true("userFunc" %in% df$call) |
| 349 |
}) |
| 350 |
|