Skip to content

Commit f920707

Browse files
authored
Report base error class (#2307)
In #2283 we started entracing snapshots so that we get useful backtraces if snapshot code unexpectedly errors. This had the unfortunate side-effect of changing the reported class for expected errors. We now show the original message if it was an entraced error.
1 parent ceeadda commit f920707

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat 3.3.1
22

3+
* `expect_snapshot()` now reports the original error class for base errors, rather than `rlang_error` (#2286).
34
* `expect_success()` and `expect_failure()` are more clear about what the expectation actually did (#2297).
45
* The hint to use `snapshot_download_gh()` is now only emitted when running in a job named "R-CMD-check" (#2300).
56
* `expect_snapshot_file()` correctly reports file name if duplicated (@MichaelChirico, #2296).

R/snapshot.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,22 @@ snapshot_replay.condition <- function(
185185
}
186186

187187
if (cnd_class) {
188-
type <- paste0(type, " <", class(x)[[1]], ">")
188+
type <- paste0(type, " <", error_class(x), ">")
189189
}
190190

191191
c(snap_header(state, type), snapshot_lines(msg, transform))
192192
}
193193

194+
error_class <- function(x) {
195+
# If error was entraced from base R error, use original error class
196+
# This is a little fragile because entrace() does not document this behaviour
197+
if (inherits(x, "rlang_error") && !is.null(x$error)) {
198+
x <- x$error
199+
}
200+
class(x)[[1]]
201+
}
202+
203+
194204
snapshot_lines <- function(x, transform = NULL) {
195205
x <- split_lines(x)
196206
if (!is.null(transform)) {

tests/testthat/_snaps/snapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
Condition <simpleWarning>
244244
Warning in `f()`:
245245
bar
246-
Condition <rlang_error>
246+
Condition <simpleError>
247247
Error in `f()`:
248248
! baz
249249

tests/testthat/test-snapshot.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ test_that("errors and warnings are folded", {
169169
# expect_snapshot(1 + 1)
170170
# })
171171

172+
test_that("extracts original error class", {
173+
catch_entraced <- function(code) {
174+
tryCatch(
175+
withCallingHandlers(code, error = function(cnd) rlang::entrace(cnd)),
176+
error = function(cnd) cnd
177+
)
178+
}
179+
180+
cnd <- catch_entraced(stop("!", call. = FALSE))
181+
expect_equal(error_class(cnd), "simpleError")
182+
183+
cnd <- catch_entraced(stop(errorCondition("!", class = "badError")))
184+
expect_equal(error_class(cnd), "badError")
185+
186+
cnd <- catch_entraced(abort("!"))
187+
expect_equal(error_class(cnd), "rlang_error")
188+
})
189+
172190
test_that("hint is informative", {
173191
local_mocked_bindings(in_check_reporter = function() FALSE)
174192
withr::local_envvar(GITHUB_ACTIONS = "false", TESTTHAT_WD = NA)

0 commit comments

Comments
 (0)