Skip to content

Commit 62ee202

Browse files
authored
Merge pull request #141 from atorus-research/108-error-messages-for-spec_to_metacore
Closes #108 error message update for `spec_to_metacore()`
2 parents f34cc5f + ac6f2c9 commit 62ee202

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

R/spec_builder.R

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -754,17 +754,27 @@ create_tbl <- function(doc, cols) {
754754
sheets_to_error <- mismatch_per_sheet %>%
755755
keep(names(.) %in% closest_sheets)
756756

757-
# Write out the error
758-
sheets_to_error %>%
759-
map2_chr(names(sheets_to_error), function(vars, sheet_name) {
760-
paste0(
761-
"Sheet '", sheet_name, "' is the closest match, but unable to match the following column(s)\n",
762-
paste(names(vars), collapse = "\n")
763-
)
764-
}) %>%
765-
paste0(collapse = "\n") %>%
766-
paste0("Unable to identify a sheet with all columns.\n", .) %>%
767-
(call. <- FALSE)
757+
# 1. Check for "where" columns
758+
has_where_col <- sheets_to_error %>%
759+
map_lgl(~ any(stringr::str_detect(names(.x), stringr::regex("^where", ignore_case = TRUE)))) %>%
760+
any()
761+
762+
# 2. Generate the formatted list of sheet matches
763+
sheet_details <- sheets_to_error %>%
764+
purrr::imap_chr(~ {
765+
paste0("Sheet '", .y, "' is missing: ", paste(names(.x), collapse = ", "))
766+
})
767+
768+
# 3. Use cli_abort with bullets to replace the standard Error: prefix
769+
cli_abort(
770+
c(
771+
"x" = "Unable to identify a sheet with all columns.",
772+
"i" = "Closest matches identified:",
773+
"*" = sheet_details,
774+
if (has_where_col) c("!" = "Tip: A 'where' column was detected. Check if {.arg where_sep_sheet} is set correctly.")
775+
),
776+
call = NULL
777+
)
768778
} else if (length(matches) == 1) {
769779
# Check names and write a better warning message if names don't work
770780
ds_nm <- matches[[1]] %>% names()

tests/testthat/test-reader.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,36 @@ test_that("define_to_metacore(quiet) deprecation message is output when supplied
612612
spec <- define_to_metacore(metacore_example("ADaM_define_CDISC_pilot3.xml"), quiet = TRUE)
613613
)
614614
})
615+
616+
test_that("Informative error when where_sep_sheet=TRUE but WhereClause sheet missing", {
617+
# This should trigger the helpful error message about where_sep_sheet
618+
expect_error(
619+
spec_to_metacore(
620+
"spec_no_val.xlsx", # Use relative path like the existing test
621+
where_sep_sheet = TRUE, # This is the default, but being explicit
622+
verbose = "silent"
623+
),
624+
regexp = "where.*where_sep_sheet",
625+
ignore.case = TRUE
626+
)
627+
628+
# Verify the error message contains helpful context
629+
err <- tryCatch(
630+
spec_to_metacore(
631+
"spec_no_val.xlsx",
632+
where_sep_sheet = TRUE,
633+
verbose = "silent"
634+
),
635+
error = function(e) conditionMessage(e)
636+
)
637+
638+
# Check that the error message mentions:
639+
# 1. That columns couldn't be matched
640+
expect_match(err, "Unable to identify a sheet|Could not find matching columns", ignore.case = TRUE)
641+
642+
# 2. Provides the helpful tip about where_sep_sheet
643+
expect_match(err, "where_sep_sheet", ignore.case = TRUE)
644+
645+
# 3. Shows which sheet was closest
646+
expect_match(err, "Sheet|Closest", ignore.case = TRUE)
647+
})

0 commit comments

Comments
 (0)