Skip to content

Move error signalling to rlang::abort() #3526

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

Merged
merged 6 commits into from
Dec 17, 2019

Conversation

thomasp85
Copy link
Member

This PR changes all calls to stop() to abort() from rlang. It further removes the last calls to stopifnot() in favour of if (...) abort(...) which should hopefully result in some better error messages (as well as consistent error tracing across all exceptions)

@yutannihilation
Copy link
Member

yutannihilation commented Sep 9, 2019

Basically looks good to me! Two minor comments.

  1. According to 2.4.1 Inline statements of The tidyverse style guide,

Function calls that affect control flow (like return(), stop() or continue) should always go in their own {} block

So I think all abort() after a if statement should go after a line break.

  1. Do you want to prohibit the internal use of stop() and stopifnot() in the same way as data.frame()? If so, we might need a better mechanism to check the code statically (it would be beyond this PR, though).

@clauswilke
Copy link
Member

And one comment from my side: Should all the new error messages in code that replaces stopifnot() follow the tidyverse style, with a statement of what is wrong and a suggestion of what might have happened?

@thomasp85
Copy link
Member Author

I’ll fix 1. As for 2. I discussed this with Hadley and my feeling is that it is too likely that something will slip through and the user will be getting a don’t use stop() error message instead of the intended one. I think we just have to rely on good reviews

Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

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

I've read over all changes. I didn't notice any issues except the minor typos I indicated.

@yutannihilation
Copy link
Member

yutannihilation commented Sep 10, 2019

it is too likely that something will slip through and the user will be getting a don’t use stop() error message instead of the intended one

Yes. I meant, so we might need to check the code programmatically on CI. For example:

library(testthat)

get_stop <- function(f) {
  d <- getParseData(parse(f))
  d[d$token == "SYMBOL_FUNCTION_CALL" & d$text == "stop", ]
}


test_that("do not use stop()", {
  stops <- purrr::map_dfr(list.files("R", full.names = TRUE), get_stop, .id = "file")
  
  expect_equal(nrow(stops), 0)  
})
#> Error: Test failed: 'do not use stop()'
#> * nrow(stops) not equal to 0.
#> 1/1 mismatches
#> [1] 173 - 0 == 173

@thomasp85
Copy link
Member Author

thomasp85 commented Sep 11, 2019

I'm putting this on ice until the next rlang release (@lionel- said it would be soon) that will include an immediate. argument (or equivalent) in warn()

@thomasp85 thomasp85 added this to the ggplot2 3.3.0 milestone Sep 27, 2019
@yutannihilation
Copy link
Member

I'm putting this on ice until the next rlang release

Is this the version?

https://github.com/r-lib/rlang/releases/tag/v0.4.1

@thomasp85
Copy link
Member Author

yeah, but immediate printing of warning turns out to not be possible to implement with the slang conditions... don't know if it is really that necessary

@lionel-
Copy link
Member

lionel- commented Nov 4, 2019

It's impossible with any condition object, not just rlang. It's possibly a bug in base::warning().

@thomasp85
Copy link
Member Author

fair - didn't meant to call rlang out

@lionel-
Copy link
Member

lionel- commented Nov 4, 2019

Bug report submitted at https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17646

@thomasp85 thomasp85 merged commit 660aad2 into tidyverse:master Dec 17, 2019
@thomasp85
Copy link
Member Author

Team (@hadley, @clauswilke, @yutannihilation, @karawoo, @paleolimbot). With the merge of this PR ggplot2 now exclusively uses abort() and warn() from rlang for condition signalling. Unit tests have been set up that scans the R folder for usage of stop() and warning() and will fail if it finds any. message() remains for now (i.e. we do not use inform() from rlang), but that is not set in stone for the future.

Further, the style now is to rely on glue to compose the messages inside conditions, to the extent possible... It is not checked, so be aware of this when reviewing PRs

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

Successfully merging this pull request may close these issues.

4 participants