Skip to content

Commit 6f4612f

Browse files
authored
Use cli messaging (#431)
* Use `cli::cli_abort()` for all errors. * Adjust test + add snapshot * Last message conversion to cli.
1 parent 42e927a commit 6f4612f

File tree

8 files changed

+28
-21
lines changed

8 files changed

+28
-21
lines changed

R/as_xml_document.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ as_xml_document.response <- read_xml.response
4040
#' @export
4141
as_xml_document.list <- function(x, ...) {
4242
if (length(x) > 1) {
43-
abort("Root nodes must be of length 1")
43+
cli::cli_abort("Root nodes must be of length 1.")
4444
}
4545

4646

R/paths.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ zipfile <- function(path, open = "r") {
5555
file <- files$Name[[1]]
5656

5757
if (nrow(files) > 1) {
58-
message("Multiple files in zip: reading '", file, "'")
58+
cli::cli_inform("Multiple files in zip: reading {.file {file}}")
5959
}
6060

6161
unz(path, file, open = open)

R/utils.R

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ s_quote <- function(x) paste0("'", x, "'")
4242
# Similar to match.arg, but returns character() with NULL or empty input and
4343
# errors if any of the inputs are not found (fixing
4444
# https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16659)
45-
parse_options <- function(arg, options) {
45+
parse_options <- function(arg, options, error_call = caller_env()) {
4646
if (is.numeric(arg)) {
4747
return(as.integer(arg))
4848
}
@@ -54,15 +54,13 @@ parse_options <- function(arg, options) {
5454
# set duplicates.ok = TRUE so any duplicates are counted differently than
5555
# non-matches, then take only unique results
5656
i <- pmatch(arg, names(options), duplicates.ok = TRUE)
57-
if (any(is.na(i))) {
58-
stop(
59-
sprintf(
60-
"`options` %s is not a valid option, should be one of %s",
61-
s_quote(arg[is.na(i)][1L]),
62-
paste(s_quote(names(options)), collapse = ", ")
63-
),
64-
call. = FALSE
65-
)
57+
if (anyNA(i)) {
58+
cli::cli_abort(c(
59+
x = "{.arg options} {.val {arg[is.na(i)][1L]}} is not a valid option.",
60+
i = "Valid options are one of {.or {.val {names(options)}}}.",
61+
i = "See {.help [read_html](xml2::read_html)} for all options."
62+
),
63+
call = error_call)
6664
}
6765
sum(options[unique(i)])
6866
}

R/xml_missing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ as.character.xml_missing <- function(x, ...) {
2828
`[.xml_missing` <- function(x, i, ...) x
2929

3030
#' @export
31-
`[[.xml_missing` <- function(x, i, ...) if (i == 1L) x else abort("subscript out of bounds")
31+
`[[.xml_missing` <- function(x, i, ...) if (i == 1L) x else cli::cli_abort("subscript out of bounds")
3232

3333
#' @export
3434
is.na.xml_missing <- function(x) {

R/xml_serialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ xml_unserialize <- function(connection, ...) {
7272
}
7373
res <- read_xml_int(unclass(object), ...)
7474
} else {
75-
abort("Not a serialized xml2 object")
75+
cli::cli_abort("Not a serialized xml2 object.")
7676
}
7777
res
7878
}

R/xml_write.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ write_xml <- function(x, file, ...) {
3030

3131
#' @export
3232
write_xml.xml_missing <- function(x, file, ...) {
33-
abort("Missing data cannot be written")
33+
cli::cli_abort("Missing data cannot be written.")
3434
}
3535

3636
#' @rdname write_xml
@@ -56,7 +56,7 @@ write_xml.xml_document <- function(x, file, ..., options = "format", encoding =
5656
#' @export
5757
write_xml.xml_nodeset <- function(x, file, ..., options = "format", encoding = "UTF-8") {
5858
if (length(x) != 1) {
59-
abort("Can only save length 1 node sets")
59+
cli::cli_abort("Can only save length 1 node sets.")
6060
}
6161

6262
options <- parse_options(options, xml_save_options())
@@ -104,7 +104,7 @@ write_html <- function(x, file, ...) {
104104

105105
#' @export
106106
write_html.xml_missing <- function(x, file, ...) {
107-
abort("Missing data cannot be written")
107+
cli::cli_abort("Missing data cannot be written.")
108108
}
109109

110110
#' @rdname write_xml

tests/testthat/_snaps/xml_parse.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
`x` must be a single string, not an empty character vector.
44

5+
# parse_options errors when given an invalid option
6+
7+
Code
8+
read_html(test_path("lego.html.bz2"), options = "INVALID")
9+
Condition
10+
Error in `read_html()`:
11+
x `options` "INVALID" is not a valid option.
12+
i Valid options are one of "RECOVER", "NOENT", "DTDLOAD", "DTDATTR", "DTDVALID", "NOERROR", "NOWARNING", "PEDANTIC", "NOBLANKS", "SAX1", "XINCLUDE", "NONET", "NODICT", "NSCLEAN", "NOCDATA", "NOXINCNODE", "COMPACT", "OLD10", ..., "IGNORE_ENC", or "BIG_LINES".
13+
i See read_html (`?xml2::read_html()`) for all options.
14+
515
# read_xml and read_html fail with > 1 input
616

717
`x` must be a single string, not a character vector.

tests/testthat/test-xml_parse.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ test_that("read_html correctly parses malformed document", {
2626
test_that("parse_options errors when given an invalid option", {
2727
expect_error(
2828
parse_options("INVALID", xml_parse_options()),
29-
"`options` 'INVALID' is not a valid option"
29+
'`options` "INVALID" is not a valid option'
3030
)
3131

32-
expect_error(
33-
read_html(test_path("lego.html.bz2"), options = "INVALID"),
34-
"`options` 'INVALID' is not a valid option"
32+
expect_snapshot(error = TRUE,
33+
read_html(test_path("lego.html.bz2"), options = "INVALID")
3534
)
3635

3736
# Empty inputs returned as 0

0 commit comments

Comments
 (0)