|
| 1 | +#' Check for unsupported argument values |
| 2 | +#' |
| 3 | +#' The function checks whether a given argument is unsupported |
| 4 | +#' It throws an error message if the argument is not among the allowed values. |
| 5 | +#' Function is taken from dplyr. |
| 6 | +#' |
| 7 | +#' @param x The argument to check. |
| 8 | +#' @param allowed A value or vector of values that are allowed for `x`. If `NULL`, no value is allowed. |
| 9 | +#' @param allow_null Logical. If `TRUE`, allows `x` to be `NULL`. |
| 10 | +#' @param backend Optional character string indicating the back-end. |
| 11 | +#' @param arg The name of the argument being checked (automatically inferred via `rlang::caller_arg()`). |
| 12 | +#' @param call The calling environment used for error reporting (automatically inferred via `rlang::caller_env()`). |
| 13 | +#' |
| 14 | +#' @noRd |
| 15 | +check_unsupported_arg <- function (x, allowed = NULL, allow_null = FALSE, backend = NULL, |
| 16 | + arg = caller_arg(x), call = caller_env()) |
| 17 | +{ |
| 18 | + if (rlang::is_missing(x)) { |
| 19 | + return() |
| 20 | + } |
| 21 | + if (allow_null && rlang::is_null(x)) { |
| 22 | + return() |
| 23 | + } |
| 24 | + if (identical(x, allowed)) { |
| 25 | + return() |
| 26 | + } |
| 27 | + if (rlang::is_null(allowed)) { |
| 28 | + msg <- "Argument {.arg {arg}} isn't supported" |
| 29 | + } |
| 30 | + else { |
| 31 | + msg <- "{.code {arg} = {.val {x}}} isn't supported" |
| 32 | + } |
| 33 | + if (is.null(backend)) { |
| 34 | + msg <- paste0(msg, " on database backends.") |
| 35 | + } |
| 36 | + else { |
| 37 | + msg <- paste0(msg, " in {backend} translation.") |
| 38 | + } |
| 39 | + if (!rlang::is_null(allowed)) { |
| 40 | + if (allow_null) { |
| 41 | + allow_msg <- "It must be {.val {allowed}} or {.code NULL} instead." |
| 42 | + } |
| 43 | + else { |
| 44 | + allow_msg <- "It must be {.val {allowed}} instead." |
| 45 | + } |
| 46 | + msg <- c(msg, i = allow_msg) |
| 47 | + } |
| 48 | + cli_abort(msg, call = call) |
| 49 | +} |
| 50 | + |
0 commit comments