-
Notifications
You must be signed in to change notification settings - Fork 129
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
subset() does not return error message properly #745
Comments
Can you use rlang::cnd_message(testthat::capture_error(
subset(tibble::tibble(a = 1), select = "b")
))
#> [1] "Can't subset columns that don't exist.\nx The column `b` doesn't exist." Created on 2020-03-24 by the reprex package (v0.3.0) It's much safer to test for the class of the condition. What's your use case? CC @lionel-. |
Do you think it would be safe to always use Thanks! |
It seems that |
conditionMessage.condition
#> function (c)
#> c$message
#> <bytecode: 0x56488f229120>
#> <environment: namespace:base> Created on 2020-03-24 by the reprex package (v0.3.0) |
The errors are new because we only started to use classed error messages in tibble 3.0.0. It should be safe to use |
I'll probably have it generate both a test of the message and the class then. I don't see the class(testthat::capture_error(
subset(tibble::tibble(a = 1), select = "b")
))
#> [1] "vctrs_error_subscript_oob" "vctrs_error_subscript"
#> [3] "rlang_error" "error"
#> [5] "condition" Created on 2020-03-24 by the reprex package (v0.3.0) |
Some errors are generated by vctrs. |
Ah, okay, so what you're saying is that if the I aim to have Thanks for the clarifications :) |
Empty message fields will become more prevalent as we move towards lazy generation of error messages. I'd use |
Let me know if this needs a separate issue, but it's pretty related. Based on #410
I'm using this: cnd_message <- function(x){
withr::local_options(c(rlang_backtrace_on_error = "none",
crayon.enabled = FALSE))
conditionMessage(x)
} I could keep gsub("\033[31mx\033[39m", "", error_msg, fixed = TRUE) Seems like a hack though and in case other packages than Any tips? |
Can you disable crayon also for the code that triggers the error message? tibble currently doesn't do lazy generation of error messages. |
I can set the > conditionMessage(testthat::capture_error(df[[NA]]))
[1] "Must extract column with a single valid subscript.\n\033[31mx\033[39m The subscript `NA` can't be `NA`."
>
> options(crayon.enabled = F)
> conditionMessage(testthat::capture_error(df[[NA]]))
[1] "Must extract column with a single valid subscript.\nx The subscript `NA` can't be `NA`." Now, I get that the ansi code is there to style the |
Can you clear the package cache on Travis? |
This comment has been minimized.
This comment has been minimized.
It fails for all but this branch:
Given that it's for Let me know if I'm venturing a bit too far off |
All conditions in tibble now have a class, so that you don't need to verify error messages at all but can compare the class. This is much more robust, much more can go wrong when comparing textual output. Perhaps xpectr should do the same and generate tests that only compare the class attribute? |
One of my own uses is to go through the generated tests ( Currently a generated test look something like this: output_10856 <- capture_side_effects(df[[NA]])
expect_equal(
strip(output_10856[["error"]]),
strip("Must extract column with a single valid subscript.\nx The subscript `NA` can't be `NA`."),
fixed = TRUE)
expect_equal(
output_10856[["error_class"]],
c("vctrs_error_subscript_type", "vctrs_error_subscript", "rlang_error",
"error", "condition"),
fixed = TRUE) The A user can remove the test of the message, if it fails for some reason. My goal is to make them useful in as many cases as possible. So if setting a locale before evaluating the code could make it work in a lot more instances, that seems like a good approach? |
You can use Have you considered generating |
Hmm, setting the locale is confusing. It seems the locale when defining # Set locale
# If I set this to en_US.ISO8859-1 it changes the encoding to latin1
Sys.setlocale(category = "LC_ALL", locale = "en_US.UTF-8")
#> [1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
f <- function(lcl){
withr::with_locale(
new = c(LC_MESSAGES = lcl, LC_COLLATE = lcl, LC_CTYPE = lcl, LC_MONETARY = lcl,
LC_TIME = lcl),
{s <- "æøå1€%§"
print(Sys.getlocale())
Encoding(s)}
)
}
f("en_US.ISO8859-1")
#> [1] "en_US.ISO8859-1/en_US.ISO8859-1/en_US.ISO8859-1/C/en_US.ISO8859-1/en_US.ISO8859-1"
#> [1] "UTF-8"
Sys.setlocale(category = "LC_ALL", locale = "en_US.ISO8859-1")
#> [1] "en_US.ISO8859-1/en_US.ISO8859-1/en_US.ISO8859-1/C/en_US.ISO8859-1/en_US.UTF-8"
f("en_US.ISO8859-1")
#> [1] "en_US.ISO8859-1/en_US.ISO8859-1/en_US.ISO8859-1/C/en_US.ISO8859-1/en_US.ISO8859-1"
#> [1] "UTF-8" Created on 2020-04-05 by the reprex package (v0.3.0 If the process that generates the message doesn't use the local locale, I would expect errors from that?
Great, I'll have a look at that :)
This seems like an option just waiting to happen. I could perhaps create a Could also add a |
I had a look at f <- function(lcl){
withr::local_envvar(c(LANGUAGE = "en"))
withr::local_envvar(c(LC_MESSAGES = lcl, LC_COLLATE = lcl,
LC_CTYPE = lcl, LC_MONETARY = lcl,
LC_TIME = lcl))
s <- "æøå1€%§" # For some reason copied as "ʯÂ1<U+20AC>%ß"
print(Sys.getlocale())
Encoding(s)
} Created on 2020-04-05 by the reprex package (v0.3.0 |
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary. |
When attempting to select a column that is not in a tibble with
subset()
, I cannot capture the error message. It works with regular data frames.I'm using the newest GitHub version of tibble (master branch). R version
3.6.1
.I've tried with
tryCatch
as well.The text was updated successfully, but these errors were encountered: