Skip to content

Commit

Permalink
Improve error handling for typos and odd calls
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Nov 6, 2023
1 parent b51d432 commit a1255fe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
21 changes: 20 additions & 1 deletion R/join-by.R
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,17 @@ parse_join_by_expr <- function(expr, i, error_call) {
expr[[1]] <- sym(call_name(expr))
}

op <- as_string(expr[[1]])
op <- expr[[1]]

if (!is_symbol(op)) {
if (is_call(op, name = "::")) {
stop_invalid_namespaced_expression(expr, i, error_call)
} else {
stop_invalid_top_expression(expr, i, error_call)
}
}

op <- as_string(op)

switch(
op,
Expand Down Expand Up @@ -481,6 +491,15 @@ stop_invalid_top_expression <- function(expr, i, call) {
abort(message, call = call)
}

stop_invalid_namespaced_expression <- function(expr, i, call) {
message <- c(
glue("Expressions can only be namespace prefixed with `dplyr::`."),
i = glue("Expression {i} is {err_expr(expr)}.")
)

abort(message, call = call)
}

parse_join_by_name <- function(expr,
i,
default_side,
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/_snaps/join-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@
! Each element of `...` must be a single column name or a join by expression.
x Element 1 is not a name and not an expression.

---

Code
join_by(1())
Condition
Error in `join_by()`:
! Expressions must use one of: `==`, `>=`, `>`, `<=`, `<`, `closest()`, `between()`, `overlaps()`, or `within()`.
i Expression 1 is `1()`.

---

Code
join_by(dplyrr::between(x, left, right))
Condition
Error in `join_by()`:
! Expressions can only be namespace prefixed with `dplyr::`.
i Expression 1 is `dplyrr::between(x, left, right)`.

---

Code
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-join-by.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ test_that("has informative error messages", {
# Garbage input
expect_snapshot(error = TRUE, join_by(1))

# Call with non-symbol first element
expect_snapshot(error = TRUE, join_by(1()))

# Namespace prefixed helper with non-dplyr namespace
# (typo or re-export, which currently isn't allowed)
expect_snapshot(error = TRUE, join_by(dplyrr::between(x, left, right)))

# Top level usage of `$`
expect_snapshot(error = TRUE, join_by(x$a))

Expand Down

0 comments on commit a1255fe

Please sign in to comment.