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

Register an rlang_error method for knitr::sew() to handle backtraces in error messages #1215

Merged
merged 3 commits into from
May 21, 2021

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented May 20, 2021

This method consults rlang_backtrace_on_error to determine whether to display a backtrace with the error message. Whereas the default value for this option is "reminder" in interactive sessions and "full" in non-interactive sessions, the default is set to "none" in knitr chunks that expect errors (error = TRUE chunks).

The method also sets rlang::last_error(). Consulting the last error must be done in a different chunk since the sew() generic is run at the end of a chunk.

This should fix all corner cases regarding backtrace handling in reprex::reprex(). Closes #1205. cc @jennybc.

I will also look into setting rlang_trace_top_env from knitr. Would there be a downside to do this @hadley?

@lionel- lionel- requested a review from hadley May 20, 2021 17:09
@lionel-
Copy link
Member Author

lionel- commented May 20, 2021

I will also look into setting rlang_trace_top_env from knitr. Would there be a downside to doing this?

oh hmm now I remember your idea in #932

We could use knitr::knit_global() to access the chunk environment. I'll look into this tomorrow.

# Save the unhandled error for `rlang::last_error()`.
last_error_env$cnd <- x

# By default, we display no reminder or backtrace for errors
Copy link
Member

Choose a reason for hiding this comment

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

What about errors not captured by knitr?

Copy link
Member Author

Choose a reason for hiding this comment

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

As before they fall through the normal case and display a reminder in interactive sessions, or a backtrace in non-interactive sessions:

Quitting from lines 34-36 (test-trace-full.Rmd) 
Error: foo
Backtrace:1. └─global::f()
 2.   └─global::g()
 3.     └─global::h()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cnd_message() is called twice in rlang::abort() within reprex::reprex()
2 participants