Skip to content

Commit ace0afd

Browse files
committed
Use "actual vs expected" phrasing in OOB errors
1 parent 4f2b172 commit ace0afd

File tree

6 files changed

+38
-28
lines changed

6 files changed

+38
-28
lines changed

R/subscript-loc.R

+11-1
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,19 @@ cnd_body_vctrs_error_subscript_oob_location <- function(cnd, ...) {
539539
n <- cnd$size
540540
elt <- cnd_subscript_element_cli(n, cnd)
541541

542+
arg <- cnd_subscript_arg(cnd)
543+
544+
if (length(i) == 1) {
545+
arg <- arg
546+
not <- glue::glue(", not {i}")
547+
} else {
548+
arg <- glue::glue("Locations in {arg}")
549+
not <- ""
550+
}
551+
542552
# TODO: Switch to `format_inline()` and format bullets lazily through rlang
543553
cli::format_error(c(
544-
"i" = "{cli::qty(n_loc)} Location{?s} {oob_enum} do{?esn't/n't} exist.",
554+
"x" = "{cli::qty(n_loc)} Location{?s} must be less than or equal to {n}{not}.",
545555
"i" = "There {cli::qty(n)} {?is/are} only {elt}."
546556
))
547557
}

tests/testthat/_snaps/error-call.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
<error/vctrs_error_subscript_oob>
209209
Error in `my_function()`:
210210
! Can't subset elements past the end.
211-
i Location 10 doesn't exist.
211+
x Location must be less than or equal to 2, not 10.
212212
i There are only 2 elements.
213213

214214
---

tests/testthat/_snaps/slice-assign.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<error/vctrs_error_subscript_oob>
3939
Error:
4040
! Can't assign to elements past the end.
41-
i Location 5 doesn't exist.
41+
x Location must be less than or equal to 3, not 5.
4242
i There are only 3 elements.
4343
Code
4444
(expect_error(vec_assign(1:3, "foo", 10), "unnamed vector"))
@@ -53,7 +53,7 @@
5353
<error/vctrs_error_subscript_oob>
5454
Error:
5555
! Can't negate elements past the end.
56-
i Location 100 doesn't exist.
56+
x Location must be less than or equal to 26, not 100.
5757
i There are only 26 elements.
5858
Code
5959
(expect_error(vec_assign(set_names(letters), "foo", "bar"), class = "vctrs_error_subscript_oob")

tests/testthat/_snaps/slice-chop.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
<error/vctrs_error_subscript_oob>
229229
Error:
230230
! Can't subset elements past the end.
231-
i Location 3 doesn't exist.
231+
x Location must be less than or equal to 2.
232232
i There are only 2 elements.
233233

234234
---

tests/testthat/_snaps/slice.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
<error/vctrs_error_subscript_oob>
2525
Error in `vec_slice()`:
2626
! Can't subset elements past the end.
27-
i Location 3 doesn't exist.
27+
x Location must be less than or equal to 2, not 3.
2828
i There are only 2 elements.
2929
Code
3030
(expect_error(vec_slice(1:2, -3L), class = "vctrs_error_subscript_oob"))
3131
Output
3232
<error/vctrs_error_subscript_oob>
3333
Error in `vec_slice()`:
3434
! Can't negate elements past the end.
35-
i Location 3 doesn't exist.
35+
x Location must be less than or equal to 2, not 3.
3636
i There are only 2 elements.
3737

3838
# can slice with double indices
@@ -89,7 +89,7 @@
8989
Condition
9090
Error in `vec_slice()`:
9191
! Can't subset elements past the end.
92-
i Locations 100 and 1000 don't exist.
92+
x Locations must be less than or equal to 26.
9393
i There are only 26 elements.
9494

9595
---
@@ -99,7 +99,7 @@
9999
Condition
100100
Error in `vec_slice()`:
101101
! Can't subset elements past the end.
102-
i Locations 100, 101, 102, ..., 109, and 110 don't exist.
102+
x Locations must be less than or equal to 26.
103103
i There are only 26 elements.
104104

105105
---

tests/testthat/_snaps/subscript-loc.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,23 @@
172172
<error/vctrs_error_subscript_oob>
173173
Error:
174174
! Can't subset elements past the end.
175-
i Location 10 doesn't exist.
175+
x Location must be less than or equal to 2, not 10.
176176
i There are only 2 elements.
177177
Code
178178
(expect_error(vec_as_location(-10L, 2L), class = "vctrs_error_subscript_oob"))
179179
Output
180180
<error/vctrs_error_subscript_oob>
181181
Error:
182182
! Can't negate elements past the end.
183-
i Location 10 doesn't exist.
183+
x Location must be less than or equal to 2, not 10.
184184
i There are only 2 elements.
185185
Code
186186
(expect_error(vec_as_location2(10L, 2L), class = "vctrs_error_subscript_oob"))
187187
Output
188188
<error/vctrs_error_subscript_oob>
189189
Error:
190190
! Can't extract elements past the end.
191-
i Location 10 doesn't exist.
191+
x Location must be less than or equal to 2, not 10.
192192
i There are only 2 elements.
193193
Code
194194
# Character indexing
@@ -462,7 +462,7 @@
462462
Condition
463463
Error:
464464
! Can't negate elements past the end.
465-
i Location 4 doesn't exist.
465+
x Location must be less than or equal to 3, not 4.
466466
i There are only 3 elements.
467467

468468
---
@@ -472,7 +472,7 @@
472472
Condition
473473
Error:
474474
! Can't negate elements past the end.
475-
i Location 4 doesn't exist.
475+
x Location must be less than or equal to 3, not 4.
476476
i There are only 3 elements.
477477

478478
# num_as_location() errors on disallowed zeros when inverting negatives (#1612)
@@ -502,7 +502,7 @@
502502
Condition
503503
Error:
504504
! Can't negate elements past the end.
505-
i Location 6 doesn't exist.
505+
x Location must be less than or equal to 5, not 6.
506506
i There are only 5 elements.
507507

508508
---
@@ -512,7 +512,7 @@
512512
Condition
513513
Error:
514514
! Can't negate elements past the end.
515-
i Location 7 doesn't exist.
515+
x Location must be less than or equal to 5, not 7.
516516
i There are only 5 elements.
517517

518518
---
@@ -522,7 +522,7 @@
522522
Condition
523523
Error:
524524
! Can't negate elements past the end.
525-
i Location 7 doesn't exist.
525+
x Location must be less than or equal to 5, not 7.
526526
i There are only 5 elements.
527527

528528
# num_as_location() with `oob = 'error'` reports negative and positive oob values
@@ -532,7 +532,7 @@
532532
Condition
533533
Error:
534534
! Can't subset elements past the end.
535-
i Locations 6 and 7 don't exist.
535+
x Locations must be less than or equal to 5.
536536
i There are only 5 elements.
537537

538538
# missing values are supported in error formatters
@@ -544,7 +544,7 @@
544544
<error/vctrs_error_subscript_oob>
545545
Error:
546546
! Can't subset elements past the end.
547-
i Locations 2 and 3 don't exist.
547+
x Locations must be less than or equal to 1.
548548
i There is only 1 element.
549549
Code
550550
(expect_error(num_as_location(c(1, NA, 3), 1, oob = "extend"), class = "vctrs_error_subscript_oob")
@@ -893,7 +893,7 @@
893893
<error/vctrs_error_subscript_oob>
894894
Error in `my_function()`:
895895
! Can't subset elements past the end.
896-
i Location 30 doesn't exist.
896+
x Location must be less than or equal to 26, not 30.
897897
i There are only 26 elements.
898898
Code
899899
(expect_error(vec_as_location("foo", NULL, letters, arg = "foo", call = call(
@@ -919,7 +919,7 @@
919919
<error/vctrs_error_subscript_oob>
920920
Error in `vec_slice()`:
921921
! Can't rename columns that don't exist.
922-
i Location 30 doesn't exist.
922+
x Location must be less than or equal to 26, not 30.
923923
i There are only 26 columns.
924924
Code
925925
(expect_error(with_tibble_cols(vec_slice(set_names(letters), -30)), class = "vctrs_error_subscript_oob")
@@ -928,7 +928,7 @@
928928
<error/vctrs_error_subscript_oob>
929929
Error in `vec_slice()`:
930930
! Can't rename columns that don't exist.
931-
i Location 30 doesn't exist.
931+
x Location must be less than or equal to 26, not 30.
932932
i There are only 26 columns.
933933
Code
934934
# With tibble rows
@@ -946,7 +946,7 @@
946946
<error/vctrs_error_subscript_oob>
947947
Error in `vec_slice()`:
948948
! Can't remove rows past the end.
949-
i Locations 27, 28, 29, and 30 don't exist.
949+
x Locations must be less than or equal to 26.
950950
i There are only 26 rows.
951951
Code
952952
(expect_error(with_tibble_rows(vec_slice(set_names(letters), -(1:30))), class = "vctrs_error_subscript_oob")
@@ -955,7 +955,7 @@
955955
<error/vctrs_error_subscript_oob>
956956
Error in `vec_slice()`:
957957
! Can't remove rows past the end.
958-
i Locations 27, 28, 29, and 30 don't exist.
958+
x Locations must be less than or equal to 26.
959959
i There are only 26 rows.
960960
Code
961961
# With tidyselect select
@@ -973,7 +973,7 @@
973973
<error/vctrs_error_subscript_oob>
974974
Error in `vec_slice()`:
975975
! Can't select columns past the end.
976-
i Location 30 doesn't exist.
976+
x Location must be less than or equal to 26, not 30.
977977
i There are only 26 columns.
978978
Code
979979
(expect_error(with_tidyselect_select(vec_slice(set_names(letters), -(1:30))),
@@ -982,7 +982,7 @@
982982
<error/vctrs_error_subscript_oob>
983983
Error in `vec_slice()`:
984984
! Can't select columns past the end.
985-
i Locations 27, 28, 29, and 30 don't exist.
985+
x Locations must be less than or equal to 26.
986986
i There are only 26 columns.
987987
Code
988988
# With tidyselect relocate
@@ -1000,7 +1000,7 @@
10001000
<error/vctrs_error_subscript_oob>
10011001
Error in `vec_slice()`:
10021002
! Can't relocate columns that don't exist.
1003-
i Location 30 doesn't exist.
1003+
x Location must be less than or equal to 26, not 30.
10041004
i There are only 26 columns.
10051005
Code
10061006
(expect_error(with_tidyselect_relocate(vec_slice(set_names(letters), -(1:30))),
@@ -1009,7 +1009,7 @@
10091009
<error/vctrs_error_subscript_oob>
10101010
Error in `vec_slice()`:
10111011
! Can't relocate columns that don't exist.
1012-
i Locations 27, 28, 29, and 30 don't exist.
1012+
x Locations must be less than or equal to 26.
10131013
i There are only 26 columns.
10141014

10151015
# vec_as_location() checks dimensionality

0 commit comments

Comments
 (0)