Skip to content

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

Merged
thomasp85 merged 6 commits into
tidyverse:masterfrom
thomasp85:rlang-abort
Dec 17, 2019
Merged

thomasp85 merged 6 commits into
tidyverse:masterfrom
thomasp85:rlang-abort

Conversation

@thomasp85

Copy link
Copy Markdown
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

yutannihilation commented Sep 9, 2019 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

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.

Comment thread R/compat-plyr.R Resolved
Comment thread R/layout.R Resolved
Comment thread R/limits.r Resolved
@yutannihilation

yutannihilation commented Sep 10, 2019 •

Copy link
Copy Markdown
Member

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

thomasp85 commented Sep 11, 2019 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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-

lionel- commented Nov 4, 2019

Copy link
Copy Markdown
Member

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

@thomasp85

Copy link
Copy Markdown
Member Author

fair - didn't meant to call rlang out

@lionel-

lionel- commented Nov 4, 2019

Copy link
Copy Markdown
Member

@thomasp85
thomasp85 merged commit 660aad2 into tidyverse:master Dec 17, 2019
@thomasp85

Copy link
Copy Markdown
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