Skip to content

FR: Error on empty selection allow_empty = FALSE has informative class #347

@eutwt

Description

@eutwt

Many of the eval_select() errors have a class that lets you know what went wrong, but not the error you get on an empty selection with allow_empty = FALSE. This would be useful if people want to rethrow the error so that the "Must select at least one item" message is more informative in the context of their function, especially to disambiguate the source of the error if there are multiple tidy-select arguments (e.g. rethrow as "Must select at least one country").

library(tidyselect)

eval_select(quote(any_of('z')), list(x = 1), allow_empty = FALSE) |>
  rlang::catch_cnd() |> 
  class()
#> [1] "rlang_error" "error"       "condition"

eval_select(c(a = 'x'), list(x = 1), allow_rename = FALSE) |>
  rlang::catch_cnd() |> 
  class()
#> [1] "tidyselect:::error_disallowed_rename"
#> [2] "rlang_error"                         
#> [3] "error"                               
#> [4] "condition"

eval_select('z', list(x = 1)) |>
  rlang::catch_cnd() |> 
  class()
#> [1] "vctrs_error_subscript_oob" "vctrs_error_subscript"    
#> [3] "rlang_error"               "error"                    
#> [5] "condition"

eval_select(list(1), list(x = 1)) |>
  rlang::catch_cnd() |> 
  class()
#> [1] "vctrs_error_subscript_type" "vctrs_error_subscript"     
#> [3] "rlang_error"                "error"                     
#> [5] "condition"

Created on 2023-11-30 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions