Skip to content

Commit 2b0a03b

Browse files
committed
Use chained errors for scalar location checks
1 parent 433fe78 commit 2b0a03b

File tree

2 files changed

+72
-41
lines changed

2 files changed

+72
-41
lines changed

R/subscript-loc.R

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ vec_as_location2_result <- function(i,
207207
i <- result$ok
208208

209209
if (length(i) != 1L) {
210-
return(result(err = new_error_location2_type(
210+
return(result(err = new_chained_error_location2_type(
211211
i = i,
212212
subscript_arg = arg,
213-
body = cnd_bullets_location2_need_scalar,
213+
header = cnd_header_location2_need_scalar,
214214
call = call
215215
)))
216216
}
@@ -222,10 +222,10 @@ vec_as_location2_result <- function(i,
222222

223223
if (is.na(i)) {
224224
if (!allow_missing && is.na(i)) {
225-
result <- result(err = new_error_location2_type(
225+
result <- result(err = new_chained_error_location2_type(
226226
i = i,
227227
subscript_arg = arg,
228-
body = cnd_bullets_location2_need_present,
228+
header = cnd_header_location2_need_present,
229229
call = call
230230
))
231231
} else {
@@ -235,19 +235,19 @@ vec_as_location2_result <- function(i,
235235
}
236236

237237
if (identical(i, 0L)) {
238-
return(result(err = new_error_location2_type(
238+
return(result(err = new_chained_error_location2_type(
239239
i = i,
240240
subscript_arg = arg,
241-
body = cnd_bullets_location2_need_positive,
241+
header = cnd_header_location2_need_positive,
242242
call = call
243243
)))
244244
}
245245

246246
if (!allow_negative && neg) {
247-
return(result(err = new_error_location2_type(
247+
return(result(err = new_chained_error_location2_type(
248248
i = i,
249249
subscript_arg = arg,
250-
body = cnd_bullets_location2_need_positive,
250+
header = cnd_header_location2_need_positive,
251251
call = call
252252
)))
253253
}
@@ -288,24 +288,37 @@ new_error_location2_type <- function(i,
288288
...
289289
)
290290
}
291+
new_chained_error_location2_type <- function(i,
292+
...,
293+
header = NULL,
294+
call = caller_env()) {
295+
causal <- error_cnd(
296+
i = i,
297+
header = header,
298+
...,
299+
call = NULL,
300+
use_cli_format = TRUE
301+
)
302+
new_error_location2_type(
303+
i = i,
304+
...,
305+
body = function(...) chr(),
306+
call = call,
307+
parent = causal
308+
)
309+
}
291310

292-
cnd_bullets_location2_need_scalar <- function(cnd, ...) {
293-
cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg)
294-
format_error_bullets(c(
295-
x = glue::glue_data(cnd, "{subscript_arg} must be size 1, not {length(i)}.")
296-
))
311+
cnd_header_location2_need_scalar <- function(cnd, ...) {
312+
cnd$subscript_arg <- cnd_subscript_arg(cnd)
313+
glue::glue_data(cnd, "{subscript_arg} must be size 1, not {length(i)}.")
297314
}
298-
cnd_bullets_location2_need_present <- function(cnd, ...) {
299-
cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg)
300-
format_error_bullets(c(
301-
x = glue::glue_data(cnd, "{subscript_arg} must be a location, not {obj_type_friendly(i)}.")
302-
))
315+
cnd_header_location2_need_present <- function(cnd, ...) {
316+
cnd$subscript_arg <- cnd_subscript_arg(cnd)
317+
glue::glue_data(cnd, "{subscript_arg} must be a location, not {obj_type_friendly(i)}.")
303318
}
304-
cnd_bullets_location2_need_positive <- function(cnd, ...) {
305-
cnd$subscript_arg <- append_arg("Subscript", cnd$subscript_arg)
306-
format_error_bullets(c(
307-
x = glue::glue_data(cnd, "{subscript_arg} must be a positive location, not {i}.")
308-
))
319+
cnd_header_location2_need_positive <- function(cnd, ...) {
320+
cnd$subscript_arg <- cnd_subscript_arg(cnd)
321+
glue::glue_data(cnd, "{subscript_arg} must be a positive location, not {i}.")
309322
}
310323

311324
stop_location_negative_missing <- function(i, ..., call = caller_env()) {

tests/testthat/_snaps/subscript-loc.md

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,17 @@
224224
<error/vctrs_error_subscript_type>
225225
Error:
226226
! Can't extract element with `1:2`.
227-
x Subscript `1:2` must be size 1, not 2.
227+
Caused by error:
228+
! `1:2` must be size 1, not 2.
228229
Code
229230
(expect_error(vec_as_location2(c("foo", "bar"), 2L, c("foo", "bar")), class = "vctrs_error_subscript_type")
230231
)
231232
Output
232233
<error/vctrs_error_subscript_type>
233234
Error:
234235
! Can't extract element with `c("foo", "bar")`.
235-
x Subscript `c("foo", "bar")` must be size 1, not 2.
236+
Caused by error:
237+
! `c("foo", "bar")` must be size 1, not 2.
236238
Code
237239
# Idem with custom `arg`
238240
(expect_error(vec_as_location2(1:2, 2L, arg = "foo", call = call("my_function")),
@@ -241,7 +243,8 @@
241243
<error/vctrs_error_subscript_type>
242244
Error in `my_function()`:
243245
! Can't extract element with `foo`.
244-
x Subscript `foo` must be size 1, not 2.
246+
Caused by error:
247+
! `foo` must be size 1, not 2.
245248
Code
246249
(expect_error(vec_as_location2(mtcars, 10L, arg = "foo", call = call(
247250
"my_function")), class = "vctrs_error_subscript_type"))
@@ -257,7 +260,8 @@
257260
<error/vctrs_error_subscript_type>
258261
Error in `my_function()`:
259262
! Can't extract element with `foo`.
260-
x Subscript `foo` must be size 1, not 2.
263+
Caused by error:
264+
! `foo` must be size 1, not 2.
261265

262266
# vec_as_location2() requires positive integers
263267

@@ -267,14 +271,16 @@
267271
<error/vctrs_error_subscript_type>
268272
Error:
269273
! Can't extract element with `0`.
270-
x Subscript `0` must be a positive location, not 0.
274+
Caused by error:
275+
! `0` must be a positive location, not 0.
271276
Code
272277
(expect_error(vec_as_location2(-1, 2L), class = "vctrs_error_subscript_type"))
273278
Output
274279
<error/vctrs_error_subscript_type>
275280
Error:
276281
! Can't extract element with `-1`.
277-
x Subscript `-1` must be a positive location, not -1.
282+
Caused by error:
283+
! `-1` must be a positive location, not -1.
278284
Code
279285
# Idem with custom `arg`
280286
(expect_error(vec_as_location2(0, 2L, arg = "foo", call = call("my_function")),
@@ -283,7 +289,8 @@
283289
<error/vctrs_error_subscript_type>
284290
Error in `my_function()`:
285291
! Can't extract element with `foo`.
286-
x Subscript `foo` must be a positive location, not 0.
292+
Caused by error:
293+
! `foo` must be a positive location, not 0.
287294

288295
# vec_as_location2() fails with NA
289296

@@ -294,15 +301,17 @@
294301
<error/vctrs_error_subscript_type>
295302
Error:
296303
! Can't extract element with `na_int`.
297-
x Subscript `na_int` must be a location, not an integer `NA`.
304+
Caused by error:
305+
! `na_int` must be a location, not an integer `NA`.
298306
Code
299307
(expect_error(vec_as_location2(na_chr, 1L, names = "foo"), class = "vctrs_error_subscript_type")
300308
)
301309
Output
302310
<error/vctrs_error_subscript_type>
303311
Error:
304312
! Can't extract element with `na_chr`.
305-
x Subscript `na_chr` must be a location, not a character `NA`.
313+
Caused by error:
314+
! `na_chr` must be a location, not a character `NA`.
306315
Code
307316
# Idem with custom `arg`
308317
(expect_error(vec_as_location2(na_int, 2L, arg = "foo", call = call(
@@ -311,7 +320,8 @@
311320
<error/vctrs_error_subscript_type>
312321
Error in `my_function()`:
313322
! Can't extract element with `foo`.
314-
x Subscript `foo` must be a location, not an integer `NA`.
323+
Caused by error:
324+
! `foo` must be a location, not an integer `NA`.
315325

316326
# num_as_location() optionally forbids negative indices
317327

@@ -744,31 +754,35 @@
744754
<error/vctrs_error_subscript_type>
745755
Error in `my_function()`:
746756
! Can't extract element with `foo`.
747-
x Subscript `foo` must be a positive location, not -1.
757+
Caused by error:
758+
! `foo` must be a positive location, not -1.
748759
Code
749760
(expect_error(vec_as_location2(0, 2, arg = "foo", call = call("my_function")),
750761
class = "vctrs_error_subscript_type"))
751762
Output
752763
<error/vctrs_error_subscript_type>
753764
Error in `my_function()`:
754765
! Can't extract element with `foo`.
755-
x Subscript `foo` must be a positive location, not 0.
766+
Caused by error:
767+
! `foo` must be a positive location, not 0.
756768
Code
757769
(expect_error(vec_as_location2(na_dbl, 2, arg = "foo", call = call(
758770
"my_function")), class = "vctrs_error_subscript_type"))
759771
Output
760772
<error/vctrs_error_subscript_type>
761773
Error in `my_function()`:
762774
! Can't extract element with `foo`.
763-
x Subscript `foo` must be a location, not an integer `NA`.
775+
Caused by error:
776+
! `foo` must be a location, not an integer `NA`.
764777
Code
765778
(expect_error(vec_as_location2(c(1, 2), 2, arg = "foo", call = call(
766779
"my_function")), class = "vctrs_error_subscript_type"))
767780
Output
768781
<error/vctrs_error_subscript_type>
769782
Error in `my_function()`:
770783
! Can't extract element with `foo`.
771-
x Subscript `foo` must be size 1, not 2.
784+
Caused by error:
785+
! `foo` must be size 1, not 2.
772786
Code
773787
(expect_error(vec_as_location(c(TRUE, FALSE), 3, arg = "foo", call = call(
774788
"my_function")), class = "vctrs_error_subscript_size"))
@@ -833,31 +847,35 @@
833847
<error/vctrs_error_subscript_type>
834848
Error:
835849
! Can't rename column with `foo(bar)`.
836-
x Subscript `foo(bar)` must be a positive location, not -1.
850+
Caused by error:
851+
! `foo(bar)` must be a positive location, not -1.
837852
Code
838853
(expect_error(with_tibble_cols(vec_as_location2(0, 2)), class = "vctrs_error_subscript_type")
839854
)
840855
Output
841856
<error/vctrs_error_subscript_type>
842857
Error:
843858
! Can't rename column with `foo(bar)`.
844-
x Subscript `foo(bar)` must be a positive location, not 0.
859+
Caused by error:
860+
! `foo(bar)` must be a positive location, not 0.
845861
Code
846862
(expect_error(with_tibble_cols(vec_as_location2(na_dbl, 2)), class = "vctrs_error_subscript_type")
847863
)
848864
Output
849865
<error/vctrs_error_subscript_type>
850866
Error:
851867
! Can't rename column with `foo(bar)`.
852-
x Subscript `foo(bar)` must be a location, not an integer `NA`.
868+
Caused by error:
869+
! `foo(bar)` must be a location, not an integer `NA`.
853870
Code
854871
(expect_error(with_tibble_cols(vec_as_location2(c(1, 2), 2)), class = "vctrs_error_subscript_type")
855872
)
856873
Output
857874
<error/vctrs_error_subscript_type>
858875
Error:
859876
! Can't rename column with `foo(bar)`.
860-
x Subscript `foo(bar)` must be size 1, not 2.
877+
Caused by error:
878+
! `foo(bar)` must be size 1, not 2.
861879
Code
862880
(expect_error(with_tibble_cols(vec_as_location(c(TRUE, FALSE), 3)), class = "vctrs_error_subscript_size")
863881
)

0 commit comments

Comments
 (0)