Skip to content

Commit

Permalink
Improve traceback
Browse files Browse the repository at this point in the history
Fixes #214
  • Loading branch information
hadley committed Aug 21, 2024
1 parent cc9f3d2 commit afc4eed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ evaluate <- function(input,
),
eval_continue = function() TRUE,
eval_stop = function() FALSE,
eval_error = function(cnd) stop(cnd)
eval_error = function(cnd) signalCondition(cnd)
)
watcher$check_devices()

Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,30 @@
Error:
! 1

# errors have useful call

Code
cnd$trace
Output
x
1. +-evaluate (local) `<fn>`()
2. | +-base::withRestarts(...) at evaluate/R/evaluate.R:144:7
3. | | \-base (local) withRestartList(expr, restarts)
4. | | +-base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]])
5. | | | \-base (local) doWithOneRestart(return(expr), restart)
6. | | \-base (local) withRestartList(expr, restarts[-nr])
7. | | +-base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]])
8. | | | \-base (local) doWithOneRestart(return(expr), restart)
9. | | \-base (local) withRestartList(expr, restarts[-nr])
10. | | \-base (local) withOneRestart(expr, restarts[[1L]])
11. | | \-base (local) doWithOneRestart(return(expr), restart)
12. | +-evaluate:::with_handlers(...) at evaluate/R/evaluate.R:144:7
13. | | +-base::eval(call) at evaluate/R/conditions.R:50:3
14. | | | \-base::eval(call)
15. | | \-base::withCallingHandlers(...)
16. | \-base::withVisible(do) at evaluate/R/evaluate.R:153:15
17. \-evaluate (local) f() at evaluate/R/evaluate.R:153:15
18. \-evaluate (local) g() at test-conditions.R:132:8
19. \-evaluate (local) h() at test-conditions.R:133:8
20. \-rlang::abort("Error") at test-conditions.R:134:8

19 changes: 19 additions & 0 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ test_that("all three starts of stop_on_error work as expected", {
expect_snapshot(evaluate('stop("1")\n2', stop_on_error = 2L), error = TRUE)
})

test_that("errors have useful call", {
f <- function() g()
g <- function() h()
h <- function() rlang::abort("Error")
cnd <- tryCatch(
evaluate('f()', stop_on_error = 2L),
error = function(cnd) cnd
)
expect_snapshot(cnd$trace)
})

test_that("errors have useful call", {
cnd <- tryCatch(
evaluate('stop("Error")', stop_on_error = 2L),
error = function(cnd) cnd
)
expect_equal(cnd$call, NULL)
})

test_that("errors during printing are captured", {
methods::setClass("A", contains = "function", where = environment())
methods::setMethod("show", "A", function(object) stop("B"))
Expand Down

0 comments on commit afc4eed

Please sign in to comment.