Skip to content

Commit

Permalink
Making debugging sitemap problems a bit easier
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and SebKrantz committed Jun 1, 2024
1 parent 3278201 commit e92ba13
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 10 additions & 4 deletions R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ build_sitemap <- function(pkg = ".") {
return()
}

cli::cli_rule("Building sitemap")
if (pkg$development$in_dev && pkg$bs_version > 3) {
url <- paste0(url, pkg$prefix)
}
Expand All @@ -38,8 +39,8 @@ build_sitemap <- function(pkg = ".") {
doc <- xml2::read_xml(
paste0("<urlset xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'></urlset>")
)

url_nodes <- purrr::map(urls, url_node)
url_nodes <- unwrap_purrr_error(purrr::map(urls, url_node))
for (url in url_nodes) {
xml2::xml_add_child(doc, url)
}
Expand All @@ -53,8 +54,13 @@ build_sitemap <- function(pkg = ".") {
}

url_node <- function(url) {
xml2::read_xml(
paste0("<url><loc>", url, "</loc></url>")
withCallingHandlers(
xml2::read_xml(
paste0("<url><loc>", url, "</loc></url>")
),
error = function(err) {
cli::cli_abort("Failed to wrap URL {.str {url}}", parent = err)
}
)
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/build-search-docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# url_node gives informative error

Code
url_node("<")
Condition
Error in `url_node()`:
! Failed to wrap URL "<"
Caused by error in `read_xml.raw()`:
! StartTag: invalid element name [68]

4 changes: 4 additions & 0 deletions tests/testthat/test-build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ test_that("build_search() builds the expected search.json with no URL", {
jsonlite::write_json(build_search_index(pkg), json_path, pretty = TRUE)
expect_snapshot_file(json_path, "search-no-url.json")
})

test_that("url_node gives informative error", {
expect_snapshot(url_node("<"), error = TRUE)
})

0 comments on commit e92ba13

Please sign in to comment.