Skip to content

Commit a0c6958

Browse files
committed
Show method name in findSpatialNeighbors error messages that come from methods #72
1 parent 07eed59 commit a0c6958

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

R/graph_wrappers.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,18 +556,19 @@ setMethod(
556556
poly2nb = poly2nb
557557
)
558558
if (length(sample_id) == 1L) {
559-
out <- .comp_graph_sample(
559+
tryCatch(out <- .comp_graph_sample(
560560
x, sample_id, type, MARGIN, method,
561561
dist_type, args, extra_args_use, glist,
562562
style, zero.policy, alpha, dmax, fun_use, return_sf
563-
)
563+
), error = function(e) stop(method, ": ", e$message, call. = FALSE))
564+
564565
} else {
565566
out <- lapply(sample_id, function(s) {
566-
.comp_graph_sample(
567+
tryCatch(.comp_graph_sample(
567568
x, s, type, MARGIN, method, dist_type,
568569
args, extra_args_use, glist, style,
569570
zero.policy, alpha, dmax, fun_use, return_sf
570-
)
571+
), error = function(e) stop(method, ": ", e$message, call. = FALSE))
571572
})
572573
names(out) <- sample_id
573574
}

tests/testthat/test-graph_wrappers.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,12 @@ test_that("Correct Visium HD graph", {
172172
g <- findVisiumHDGraph(sfe)
173173
expect_s3_class(g, "listw")
174174
})
175+
176+
test_that("Show method name in error message", {
177+
mat <- matrix(rnorm(9), 3, 3)
178+
# Get duplicated coordinates
179+
coords <- cbind(c(1,2,1), c(1,2,1))
180+
sfe <- SpatialFeatureExperiment(assays = list(counts = mat),
181+
spatialCoords = coords)
182+
expect_error(findSpatialNeighbors(sfe), "tri2nb")
183+
})

0 commit comments

Comments
 (0)