Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve traceback #218

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we now get the actual error traceback. Is there some obvious way to trim off some of the earlier frames?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can no longer use frame environments as boundaries (or top_env) because we no longer evaluate in an eval() stack frame. We might have to introduce a new way of setting a boundary in the call stack...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create an evaluate_ wrapper that we can call from evaluate() so we still get one regular frame on the call stack?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm that doesn't make any difference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you set the rlang_trace_top_env option (from memory) in that wrapper?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this in both evaluate() and evaluate_():

old <- options(rlang_trace_top_env = environment())
defer(options(old))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder if this is the root cause of #219

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
Loading