Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify config error messages #2610

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/build-article.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build_article <- function(name,

front <- rmarkdown::yaml_front_matter(input_path)
# Take opengraph from article's yaml front matter
front_opengraph <- check_open_graph(front$opengraph, input)
front_opengraph <- check_open_graph(pkg, front$opengraph, input)
Copy link
Collaborator

Choose a reason for hiding this comment

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

how many of pkgdown functions now need pkg as argument 😂

data$opengraph <- modify_list(data$opengraph, front_opengraph)

# Allow users to opt-in to their own template
Expand Down
29 changes: 21 additions & 8 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,31 @@ config_abort_type <- function(must_be, not, error_pkg, error_path, error_call) {

config_abort <- function(pkg,
message,
path = pkgdown_config_path(pkg) %||% "_pkgdown.yml",
...,
call = caller_env(),
.envir = caller_env()) {

if (is_absolute_path(path)) {
path_label <- path_rel(path, pkg$src_path)
} else {
path_label <- path
}

edit <- cli::format_inline("Edit {config_path(pkg)} to fix the problem.")

cli::cli_abort(
c(message, i = edit),
...,
call = call,
.envir = .envir
)
link <- cli::style_hyperlink(path_label, paste0("file://", path))
message[[1]] <- paste0("In ", link, ", ", message[[1]])
cli::cli_abort(message, ..., call = call, .envir = .envir)
}
config_warn <- function(pkg,
hadley marked this conversation as resolved.
Show resolved Hide resolved
message,
path = pkgdown_config_path(pkg) %||% "_pkgdown.yml",
...,
call = caller_env(),
.envir = caller_env()) {

link <- cli::style_hyperlink(path_rel(path, pkg$src_path), paste0("file://", path))
message[[1]] <- paste0("In ", link, ", ", message[[1]])
cli::cli_warn(message, ..., call = call, .envir = .envir)
}

config_path <- function(pkg) {
Expand Down
36 changes: 19 additions & 17 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ data_template <- function(pkg = ".", depth = 0L) {
data_open_graph <- function(pkg = ".", call = caller_env()) {
pkg <- as_pkgdown(pkg)
og <- config_pluck_list(pkg, "template.opengraph", default = list())
og <- check_open_graph(og, pkgdown_config_path(pkg) %||% "_pkgdown.yml", call = call)
og <- check_open_graph(pkg, og, pkgdown_config_path(pkg) %||% "_pkgdown.yml", call = call)

logo <- find_logo(pkg$src_path)
if (is.null(og$image) && !is.null(logo)) {
Expand All @@ -161,44 +161,43 @@ data_open_graph <- function(pkg = ".", call = caller_env()) {
og
}

check_open_graph <- function(og, path, call = caller_env()) {
check_open_graph <- function(pkg, og, file_path, call = caller_env()) {
if (is.null(og)) {
return()
}

is_yaml <- path_ext(path) %in% c("yml", "yaml")
is_yaml <- path_ext(file_path) %in% c("yml", "yaml")
base_path <- if (is_yaml) "template.opengraph" else "opengraph"

check_open_graph_list(
pkg,
og,
file_path = path,
file_path = file_path,
error_path = base_path,
error_call = call
)

supported_fields <- c("image", "twitter")
unsupported_fields <- setdiff(names(og), supported_fields)
if (length(unsupported_fields)) {
cli::cli_warn(
"{.file {path}}: Unsupported {.field {base_path}} {cli::qty(unsupported_fields)} field{?s}: {.val {unsupported_fields}}.",
call = call
)
msg <- "Unsupported {.field {base_path}} {cli::qty(unsupported_fields)} field{?s}: {.val {unsupported_fields}}."
config_warn(pkg, msg, path = file_path, call = call)
}
check_open_graph_list(
pkg,
og$twitter,
file_path = path,
file_path = file_path,
error_path = paste0(base_path, ".twitter"),
error_call = call
)
if (!is.null(og$twitter) && is.null(og$twitter$creator) && is.null(og$twitter$site)) {
cli::cli_abort(
"{.file {path}}: {.field opengraph.twitter} must include either {.field creator} or {.field site}.",
call = call
)
msg <- "{.field opengraph.twitter} must include either {.field creator} or {.field site}."
config_abort(pkg, msg, path = file_path, call = call)
}
check_open_graph_list(
pkg,
og$image,
file_path = path,
file_path = file_path,
error_path = paste0(base_path, ".image"),
error_call = call
)
Expand All @@ -213,16 +212,19 @@ render_template <- function(path, data) {
whisker::whisker.render(template, data)
}

check_open_graph_list <- function(x,
check_open_graph_list <- function(pkg,
x,
file_path,
error_path,
error_call = caller_env()) {
if (is.list(x) || is.null(x)) {
return()
}
not <- obj_type_friendly(x)
cli::cli_abort(
"{.file {file_path}}: {.field {error_path}} must be a list, not {not}.",
config_abort(
pkg,
"{.field {error_path}} must be a list, not {not}.",
path = file_path,
call = error_call
)
}
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/_snaps/build-article.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
build_rmarkdown_format(pkg, "article")
Condition
Error in `build_rmarkdown_format()`:
! code.width must be a whole number, not the string "abc".
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, code.width must be a whole number, not the string "abc".

# output is reproducible by default, i.e. 'seed' is respected

Expand All @@ -52,7 +51,7 @@
build_article(pkg = pkg, name = "bad-opengraph")
Condition
Error in `build_article()`:
! 'vignettes/bad-opengraph.Rmd': opengraph.twitter must be a list, not the number 1.
! In vignettes/bad-opengraph.Rmd, opengraph.twitter must be a list, not the number 1.

# render_rmarkdown copies image files in subdirectories

Expand Down
42 changes: 14 additions & 28 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,73 @@
data_articles_index_(1)
Condition
Error in `data_articles_index_()`:
! articles must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, articles must be a list, not the number 1.
Code
data_articles_index_(list(1))
Condition
Error in `data_articles_index_()`:
! articles[1] must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, articles[1] must be a list, not the number 1.
Code
data_articles_index_(list(list()))
Condition
Error in `data_articles_index_()`:
! articles[1] must have components "title" and "contents".
! In _pkgdown.yml, articles[1] must have components "title" and "contents".
2 missing components: "title" and "contents".
i Edit _pkgdown.yml to fix the problem.
Code
data_articles_index_(list(list(title = 1, contents = 1)))
Condition
Error in `data_articles_index_()`:
! articles[1].title must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, articles[1].title must be a string, not the number 1.
Code
data_articles_index_(list(list(title = "a\n\nb", contents = 1)))
Condition
Error in `data_articles_index_()`:
! articles[1].title must be inline markdown.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, articles[1].title must be inline markdown.
Code
data_articles_index_(list(list(title = "a", contents = 1)))
Condition
Error in `data_articles_index_()`:
! articles[1].contents[1] must be a string.
! In _pkgdown.yml, articles[1].contents[1] must be a string.
i You might need to add '' around special YAML values like 'N' or 'off'
i Edit _pkgdown.yml to fix the problem.

# validates external-articles

Code
data_articles_(1)
Condition
Error in `data_articles_()`:
! external-articles must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles must be a list, not the number 1.
Code
data_articles_(list(1))
Condition
Error in `data_articles_()`:
! external-articles[1] must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles[1] must be a list, not the number 1.
Code
data_articles_(list(list(name = "x")))
Condition
Error in `data_articles_()`:
! external-articles[1] must have components "name", "title", "href", and "description".
! In _pkgdown.yml, external-articles[1] must have components "name", "title", "href", and "description".
3 missing components: "title", "href", and "description".
i Edit _pkgdown.yml to fix the problem.
Code
data_articles_(list(list(name = 1, title = "x", href = "x", description = "x")))
Condition
Error in `data_articles_()`:
! external-articles[1].name must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles[1].name must be a string, not the number 1.
Code
data_articles_(list(list(name = "x", title = 1, href = "x", description = "x")))
Condition
Error in `data_articles_()`:
! external-articles[1].title must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles[1].title must be a string, not the number 1.
Code
data_articles_(list(list(name = "x", title = "x", href = 1, description = "x")))
Condition
Error in `data_articles_()`:
! external-articles[1].href must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles[1].href must be a string, not the number 1.
Code
data_articles_(list(list(name = "x", title = "x", href = "x", description = 1)))
Condition
Error in `data_articles_()`:
! external-articles[1].description must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, external-articles[1].description must be a string, not the number 1.

# articles in vignettes/articles/ are unnested into articles/

Expand All @@ -99,6 +86,5 @@
. <- data_articles_index(pkg)
Condition
Error:
! 1 vignette missing from index: "c".
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, 1 vignette missing from index: "c".

15 changes: 5 additions & 10 deletions tests/testthat/_snaps/build-footer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,25 @@
data_footer_(footer = 1)
Condition
Error in `data_footer_()`:
! footer must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, footer must be a list, not the number 1.
Code
data_footer_(footer = list(structure = 1))
Condition
Error in `data_footer_()`:
! footer.structure must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, footer.structure must be a list, not the number 1.
Code
data_footer_(footer = list(components = 1))
Condition
Error in `data_footer_()`:
! footer.components must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, footer.components must be a list, not the number 1.
Code
data_footer_(authors = list(footer = list(roles = 1)))
Condition
Error in `data_footer_()`:
! authors.footer.roles must be a character vector, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors.footer.roles must be a character vector, not the number 1.
Code
data_footer_(authors = list(footer = list(text = 1)))
Condition
Error in `data_footer_()`:
! authors.footer.text must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors.footer.text must be a string, not the number 1.

18 changes: 6 additions & 12 deletions tests/testthat/_snaps/build-home-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,35 @@
data_authors_(authors = 1)
Condition
Error in `data_authors_()`:
! authors must be a list, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors must be a list, not the number 1.
Code
data_authors_(template = list(authors = list(before = 1)))
Condition
Error in `data_authors_()`:
! template.authors.before must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, template.authors.before must be a string, not the number 1.
Code
data_authors_(template = list(authors = list(after = 1)))
Condition
Error in `data_authors_()`:
! template.authors.after must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, template.authors.after must be a string, not the number 1.

# data_home_sidebar_authors validates yaml inputs

Code
data_home_sidebar_authors_(authors = list(sidebar = list(roles = 1)))
Condition
Error in `data_home_sidebar_authors_()`:
! authors.sidebar.roles must be a character vector, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors.sidebar.roles must be a character vector, not the number 1.
Code
data_home_sidebar_authors_(authors = list(sidebar = list(before = 1)))
Condition
Error in `data_home_sidebar_authors_()`:
! authors.sidebar.before must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors.sidebar.before must be a string, not the number 1.
Code
data_home_sidebar_authors_(authors = list(sidebar = list(before = "x\n\ny")))
Condition
Error in `data_home_sidebar_authors_()`:
! authors.sidebar.before must be inline markdown.
i Edit _pkgdown.yml to fix the problem.
! In _pkgdown.yml, authors.sidebar.before must be inline markdown.

# sidebar can accept additional before and after text

Expand Down
Loading
Loading