Skip to content

Commit

Permalink
Merge pull request #1504 from olivroy/error-msg
Browse files Browse the repository at this point in the history
Improve error message for gt functions
  • Loading branch information
rich-iannone authored Dec 5, 2023
2 parents 57bad1e + f4e39a0 commit 2246c71
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* `gtsave()` now returns the file path invisibly instead of `TRUE` (@olivroy, #1478).

* Most functions in gt receive a better error message if they don't provide a `gt_tbl` as an input (@olivroy, #1504).

# gt 0.10.0

## Nanoplots
Expand Down
16 changes: 10 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ is_nonempty_string <- function(x) {
#' @param data The input `data` object that is to be validated.
#'
#' @noRd
stop_if_not_gt_tbl <- function(data) {
# Use rlang::caller_env() to inform user of the precise location of failure.
stop_if_not_gt_tbl <- function(data, call = caller_env()) {
if (!is_gt_tbl(data = data)) {
cli::cli_abort(
"The `data` provided is not a `gt_tbl` object."
"`data` must be a `gt_tbl` object, not {.obj_type_friendly {data}}.",
call = call
)
}
}
Expand All @@ -99,10 +101,11 @@ stop_if_not_gt_tbl <- function(data) {
#' @param data The input `data` object that is to be validated.
#'
#' @noRd
stop_if_not_gt_group <- function(data) {
stop_if_not_gt_group <- function(data, call = caller_env()) {
if (!is_gt_group(data = data)) {
cli::cli_abort(
"The `data` provided is not a `gt_group` object."
"`data` must be a `gt_group` object, not {.obj_type_friendly {data}}.",
call = call
)
}
}
Expand All @@ -112,10 +115,11 @@ stop_if_not_gt_group <- function(data) {
#' @param data The input `data` object that is to be validated.
#'
#' @noRd
stop_if_not_gt_tbl_or_group <- function(data) {
stop_if_not_gt_tbl_or_group <- function(data, call = caller_env()) {
if (!is_gt_tbl(data = data) && !is_gt_group(data = data)) {
cli::cli_abort(
"The `data` provided is neither a `gt_tbl` nor a `gt_group` object."
"`data` must either be a `gt_tbl` or a `gt_group`, not {.obj_type_friendly {data}}.",
call = error_call
)
}
}
Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/helper-render_formats.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Testable version of the `render_formats()` function
render_formats_test <- function(data,
context) {

data %>%
build_data(context = context) %>%
.$`_body`
build_data(data, context = context)$`_body`
}
2 changes: 1 addition & 1 deletion tests/testthat/test-input_data_validation.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("All exported functions validate the incoming `data` object", {

regexp_stop <- "The `data` provided is not a `gt_tbl` object"
regexp_stop <- "`data` must be a `gt_tbl` object"

# Test the `exibble` tibble with all exported functions;
# don't provide values for any arguments and ensure that
Expand Down

5 comments on commit 2246c71

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.