diff --git a/NEWS.md b/NEWS.md index 19ffbbf7..cb22566b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ * `parameters()` and the `grid_*()` functions give more information in the error message when non-parameter objects are passed in (#437, #438). +* Improved several type-checking error messages to include the actual type of the input (#423). + * `encode_unit()` now provides a helpful error when `x` is not a parameter object (#430). * `value_validate()`, `value_transform()`, `value_inverse()`, and `value_set()` now produce more informative error messages when values contain unknowns (#445). diff --git a/R/encode_unit.R b/R/encode_unit.R index 58986ade..0c50b917 100644 --- a/R/encode_unit.R +++ b/R/encode_unit.R @@ -19,10 +19,7 @@ encode_unit <- function(x, value, direction, ...) { #' @export encode_unit.default <- function(x, value, direction, ...) { - cli::cli_abort( - "{.arg x} should be a dials parameter object, - not {.obj_type_friendly x}." - ) + stop_input_type(x, "a dials parameter object") } #' @rdname encode_unit @@ -35,7 +32,7 @@ encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { } if (!is.numeric(value) || is.matrix(value)) { - cli::cli_abort("{.arg value} should be a numeric vector.") + stop_input_type(value, "a numeric vector") } param_rng <- x$range$upper - x$range$lower @@ -83,7 +80,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) { # convert to [0, 1] if (!is.character(value) || is.matrix(value)) { - cli::cli_abort("{.arg value} should be a character vector.") + stop_input_type(value, "a character vector") } compl <- value[!is.na(value)] @@ -107,7 +104,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) { } if (!is.numeric(value) || is.matrix(value)) { - cli::cli_abort("{.arg value} should be a numeric vector.") + stop_input_type(value, "a numeric vector") } ind <- cut( diff --git a/R/grids.R b/R/grids.R index 477262ea..f9c0d875 100644 --- a/R/grids.R +++ b/R/grids.R @@ -407,11 +407,7 @@ make_random_grid <- function( # ------------------------------------------------------------------------------ new_param_grid <- function(x = new_data_frame()) { - if (!is.data.frame(x)) { - cli::cli_abort( - "{.arg x} must be a data frame to construct a new grid from." - ) - } + check_data_frame(x) x <- vctrs::vec_unique(x) size <- vctrs::vec_size(x) diff --git a/R/misc.R b/R/misc.R index 17c1cfdf..3c45dbc9 100644 --- a/R/misc.R +++ b/R/misc.R @@ -146,7 +146,7 @@ check_values_quant <- function( } if (!is.numeric(x)) { - cli::cli_abort("{.arg {arg}} must be numeric.", call = call) + stop_input_type(x, "a numeric vector", arg = arg, call = call) } if (anyNA(x)) { diff --git a/tests/testthat/_snaps/constructors.md b/tests/testthat/_snaps/constructors.md index 992839c8..6aa1cc43 100644 --- a/tests/testthat/_snaps/constructors.md +++ b/tests/testthat/_snaps/constructors.md @@ -316,7 +316,7 @@ new_quant_param(type = "integer", values = "not_numeric", label = c(foo = "Foo")) Condition Error: - ! `values` must be numeric. + ! `values` must be a numeric vector, not the string "not_numeric". --- diff --git a/tests/testthat/_snaps/encode_unit.md b/tests/testthat/_snaps/encode_unit.md index 22bfa7ee..69e334f2 100644 --- a/tests/testthat/_snaps/encode_unit.md +++ b/tests/testthat/_snaps/encode_unit.md @@ -12,7 +12,7 @@ encode_unit(prune_method(), 13, direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a character vector. + ! `value` must be a character vector, not the number 13. # to [0, 1] for quantitative values @@ -20,7 +20,7 @@ encode_unit(penalty(), "penalty", direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a numeric vector. + ! `value` must be a numeric vector, not the string "penalty". # encode_unit validates original argument @@ -36,7 +36,7 @@ encode_unit("foo") Condition Error in `encode_unit()`: - ! `x` should be a dials parameter object, not a string. + ! `x` must be a dials parameter object, not the string "foo". --- @@ -44,7 +44,7 @@ encode_unit(2, prune_method()$values, direction = "forward") Condition Error in `encode_unit()`: - ! `x` should be a dials parameter object, not a string. + ! `x` must be a dials parameter object, not the number 2. --- @@ -61,7 +61,7 @@ encode_unit(x, prune_method()$values, direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a numeric vector. + ! `value` must be a numeric vector, not a character vector. --- @@ -69,7 +69,7 @@ encode_unit(z, 1, direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a character vector. + ! `value` must be a character vector, not the number 1. --- @@ -77,7 +77,7 @@ encode_unit(x, matrix(letters[1:4], ncol = 2), direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a numeric vector. + ! `value` must be a numeric vector, not a character matrix. --- @@ -85,7 +85,7 @@ encode_unit(x, matrix(1:4, ncol = 2), direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a numeric vector. + ! `value` must be a numeric vector, not an integer matrix. --- @@ -93,7 +93,7 @@ encode_unit(z, matrix(1:4, ncol = 2), direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a character vector. + ! `value` must be a character vector, not an integer matrix. --- @@ -101,7 +101,7 @@ encode_unit(z, matrix(letters[1:4], ncol = 2), direction = "forward") Condition Error in `encode_unit()`: - ! `value` should be a character vector. + ! `value` must be a character vector, not a character matrix. --- @@ -109,7 +109,7 @@ encode_unit(x, prune_method()$values, direction = "backward") Condition Error in `encode_unit()`: - ! `value` should be a numeric vector. + ! `value` must be a numeric vector, not a character vector. --- diff --git a/tests/testthat/_snaps/grids.md b/tests/testthat/_snaps/grids.md index 570203b8..344efc88 100644 --- a/tests/testthat/_snaps/grids.md +++ b/tests/testthat/_snaps/grids.md @@ -397,5 +397,5 @@ new_param_grid(as.matrix(x)) Condition Error in `new_param_grid()`: - ! `x` must be a data frame to construct a new grid from. + ! `x` must be a data frame, not an integer matrix. diff --git a/tests/testthat/_snaps/misc.md b/tests/testthat/_snaps/misc.md index d2f03fbe..1e8c054d 100644 --- a/tests/testthat/_snaps/misc.md +++ b/tests/testthat/_snaps/misc.md @@ -20,7 +20,7 @@ check_values_quant("should have been a numeric") Condition Error: - ! `"should have been a numeric"` must be numeric. + ! `"should have been a numeric"` must be a numeric vector, not the string "should have been a numeric". ---