diff --git a/R/sparql-protection.R b/R/sparql-protection.R index 92d02f7..bf6c9a1 100644 --- a/R/sparql-protection.R +++ b/R/sparql-protection.R @@ -7,7 +7,7 @@ #' expression. #' @export spq <- function(...) { - x <- c_character(...) + x <- c_character(..., call = rlang::caller_env()) structure(x, class = c("spq", "character")) } @@ -30,14 +30,17 @@ format.spq <- function(x, ...) { #' @export is.spq <- function(x) inherits(x, "spq") -c_character <- function(...) { +c_character <- function(..., call) { x <- c(...) if (length(x) == 0) { return(character()) } if (!is.character(x)) { - rlang::abort("Character input expected") + cli::cli_abort( + "Character input expected", + call = call + ) } x diff --git a/R/spq_assemble.R b/R/spq_assemble.R index 0b363d8..d013d3f 100644 --- a/R/spq_assemble.R +++ b/R/spq_assemble.R @@ -18,7 +18,11 @@ spq_assemble = function(.query, strict = TRUE) { # prefixes ----- prefixes_known = .query[["prefixes_provided"]] %>% dplyr::bind_rows(usual_prefixes) - check_prefixes(.query[["prefixes_used"]], prefixes_known = prefixes_known) + check_prefixes( + .query[["prefixes_used"]], + prefixes_known = prefixes_known, + call = rlang::caller_env() + ) part_prefixes <- if (nrow(.query[["prefixes_provided"]]) > 0) { glue::glue( @@ -249,6 +253,6 @@ add_label = function(vector, label, label_name, old_select) { } -check_prefixes <- function(prefixes, prefixes_known) { - purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known) +check_prefixes <- function(prefixes, prefixes_known, call) { + purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known, call = call) } diff --git a/R/spq_select.R b/R/spq_select.R index 9bfa23f..59c228e 100644 --- a/R/spq_select.R +++ b/R/spq_select.R @@ -47,7 +47,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) { if (length(plus_variables) > 0) { - check_variables_present(.query, plus_variables) + check_variables_present(.query, plus_variables, call = rlang::caller_env()) if (is.data.frame(.query[["structure"]])) { .query[["structure"]][["selected"]] = FALSE @@ -65,7 +65,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) { str_remove("\\-") if (length(minus_variables) > 0) { - check_variables_present(.query, minus_variables) + check_variables_present(.query, minus_variables, call = rlang::caller_env()) .query = purrr::reduce( minus_variables, @@ -77,7 +77,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) { return(.query) } -check_variables_present <- function(query, variables) { +check_variables_present <- function(query, variables, call) { if (nzchar(Sys.getenv("GLITTER.TESTING.SELECT"))) { return() @@ -89,6 +89,6 @@ check_variables_present <- function(query, variables) { cli::cli_abort(c( "Can't use {.fun spq_select} on absent variables: {toString(absent_variables)}.", i = "Did you forget a call to {.fun spq_add}, {.fun spq_mutate} or {.fun spq_label}?" - )) + ), call = call) } } diff --git a/R/treat-argument.R b/R/treat-argument.R index f5134e3..d56281a 100644 --- a/R/treat-argument.R +++ b/R/treat-argument.R @@ -58,7 +58,7 @@ spq_translate_dsl <- function(code) { # Abort if not sparqlish not_sparqlish = xml2::xml_find_all(code_data, ".//SYMBOL_FUNCTION_CALL[@sparqlish='false']") if (length(not_sparqlish) > 0) { - rlang::abort( + cli::cli_abort( c( x = sprintf( "Can't find SPARQL equivalent for %s().", @@ -67,7 +67,8 @@ spq_translate_dsl <- function(code) { ) ), i = "If you think there should be one, open an issue in https://github.com/lvaudor/glitter." - ) + ), + call = NULL ) } diff --git a/R/utils-str.R b/R/utils-str.R index 53b532b..bb3e2d0 100644 --- a/R/utils-str.R +++ b/R/utils-str.R @@ -46,7 +46,7 @@ is_value = function(string){ #' @examples #' check_prefix(prefixes_used=c("wd","wdt"), prefixes_known = usual_prefixes) # TRUE #' check_prefix("blop:blabla", prefixes_known=usual_prefixes) #returns error message -check_prefix = function(prefixes_used, prefixes_known) { +check_prefix = function(prefixes_used, prefixes_known, call = NULL) { unknown_prefixes <- prefixes_used[!(prefixes_used %in% prefixes_known$name)] if (length(unknown_prefixes) == 0) { return(TRUE) diff --git a/tests/testthat/_snaps/sparql-protection.md b/tests/testthat/_snaps/sparql-protection.md index 64af136..23292df 100644 --- a/tests/testthat/_snaps/sparql-protection.md +++ b/tests/testthat/_snaps/sparql-protection.md @@ -9,6 +9,7 @@ Code spq(1) - Error - Character input expected + Condition + Error: + ! Character input expected diff --git a/tests/testthat/_snaps/spq_assemble.md b/tests/testthat/_snaps/spq_assemble.md index d4642e8..a7a736e 100644 --- a/tests/testthat/_snaps/spq_assemble.md +++ b/tests/testthat/_snaps/spq_assemble.md @@ -54,8 +54,9 @@ Code spq_init() %>% spq_filter(lang(itemTitleLOOKTYPO) == "en") %>% spq_assemble() - Error - Can't filter on undefined variables: ?itemTitleLOOKTYPO + Condition + Error in `spq_assemble()`: + ! Can't filter on undefined variables: ?itemTitleLOOKTYPO i You haven't mentioned them in any triple, VALUES, mutate. # spq_assemble() called from printing isn't strict diff --git a/tests/testthat/_snaps/spq_control_request.md b/tests/testthat/_snaps/spq_control_request.md index bc6d6e7..9f3b5df 100644 --- a/tests/testthat/_snaps/spq_control_request.md +++ b/tests/testthat/_snaps/spq_control_request.md @@ -2,37 +2,42 @@ Code spq_control_request(timeout = "ahahah") - Error - Must provide an integer as `timeout`. + Condition + Error in `spq_control_request()`: + ! Must provide an integer as `timeout`. i You provided a "character". --- Code spq_control_request(max_tries = "ahahah") - Error - Must provide an integer as `max_tries`. + Condition + Error in `spq_control_request()`: + ! Must provide an integer as `max_tries`. i You provided a "character". --- Code spq_control_request(max_seconds = "ahahah") - Error - Must provide an integer as `max_seconds`. + Condition + Error in `spq_control_request()`: + ! Must provide an integer as `max_seconds`. i You provided a "character". --- Code spq_control_request(request_type = "ahahah") - Error - `request_type` must be one of "url" or "body-form", not "ahahah". + Condition + Error in `spq_control_request()`: + ! `request_type` must be one of "url" or "body-form", not "ahahah". --- Code spq_control_request(user_agent = 42) - Error - Must provide a character as `user_agent`. + Condition + Error in `spq_control_request()`: + ! Must provide a character as `user_agent`. diff --git a/tests/testthat/_snaps/spq_init.md b/tests/testthat/_snaps/spq_init.md index f8d03d0..9a93da0 100644 --- a/tests/testthat/_snaps/spq_init.md +++ b/tests/testthat/_snaps/spq_init.md @@ -33,6 +33,7 @@ Code spq_init(request_control = list(max_tries = 1L)) - Error - `request_control` must be created by `spq_control_request()`. + Condition + Error in `spq_init()`: + ! `request_control` must be created by `spq_control_request()`. diff --git a/tests/testthat/_snaps/spq_language.md b/tests/testthat/_snaps/spq_language.md index 658b122..f8a87ca 100644 --- a/tests/testthat/_snaps/spq_language.md +++ b/tests/testthat/_snaps/spq_language.md @@ -2,7 +2,8 @@ Code spq_init() %>% spq_language("en") - Warning + Condition + Warning: `spq_language()` was deprecated in glitter 0.2.0. i Please use `spq_label()` instead. i See the `.languages` argument diff --git a/tests/testthat/_snaps/spq_select.md b/tests/testthat/_snaps/spq_select.md index fb863f4..163e0e0 100644 --- a/tests/testthat/_snaps/spq_select.md +++ b/tests/testthat/_snaps/spq_select.md @@ -117,8 +117,8 @@ i In index: 1. i With name: birthyear. - Caused by error in `spq_translate_dsl()`: - ! x Can't find SPARQL equivalent for collapse(). + Caused by error: + x Can't find SPARQL equivalent for collapse(). i If you think there should be one, open an issue in https://github.com/lvaudor/glitter. # spq_select can use DISTINCT and REDUCED @@ -158,8 +158,9 @@ spq_init() %>% spq_add("?station wdt:P16 wd:Q1552") %>% spq_add( "?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>% spq_select(station_label, blop) - Error - Can't use `spq_select()` on absent variables: ?station_label, ?blop. + Condition + Error: + ! Can't use `spq_select()` on absent variables: ?station_label, ?blop. i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`? --- @@ -168,7 +169,8 @@ spq_init() %>% spq_add("?station wdt:P16 wd:Q1552") %>% spq_add( "?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>% spq_label(station) %>% spq_select(station_label, blop) - Error - Can't use `spq_select()` on absent variables: ?blop. + Condition + Error: + ! Can't use `spq_select()` on absent variables: ?blop. i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`? diff --git a/tests/testthat/_snaps/treat-argument.md b/tests/testthat/_snaps/treat-argument.md index 29fdc74..7ebc1f0 100644 --- a/tests/testthat/_snaps/treat-argument.md +++ b/tests/testthat/_snaps/treat-argument.md @@ -2,7 +2,8 @@ Code spq_treat_argument("something(bla)") - Error + Condition + Error: x Can't find SPARQL equivalent for something(). i If you think there should be one, open an issue in https://github.com/lvaudor/glitter. diff --git a/tests/testthat/_snaps/utils-str.md b/tests/testthat/_snaps/utils-str.md index 4495e57..4c67786 100644 --- a/tests/testthat/_snaps/utils-str.md +++ b/tests/testthat/_snaps/utils-str.md @@ -2,6 +2,7 @@ Code check_prefix("blop", usual_prefixes) - Error - Can't find prefix(es) blop. Please use spq_prefix(). + Condition + Error in `check_prefix()`: + ! Can't find prefix(es) blop. Please use spq_prefix().