Skip to content

Commit d8e51f2

Browse files
authored
Print class of uncaught errors (#1439)
Fixes #1426
1 parent 44fb3f2 commit d8e51f2

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# testthat (development version)
22

3+
* Uncaught errors now show their class (#1426).
4+
35
* `local_reproducible_output()` now sets the `max.print` option to 99999
46
(the default), so your tests are unaffected by any changes you might've
57
made in your `.Rprofile` (1367).

R/expectation.R

+12-2
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,25 @@ as.expectation.expectation <- function(x, srcref = NULL) {
192192

193193
#' @export
194194
as.expectation.error <- function(x, srcref = NULL) {
195+
195196
if (is.null(x$call)) {
196-
msg <- paste0("Error: ", cnd_message(x))
197+
header <- paste0("Error: ")
197198
} else {
198-
msg <- paste0("Error in `", as_label(x$call[1]), "`: ", cnd_message(x))
199+
header <- paste0("Error in `", as_label(x$call[1]), "`: ")
199200
}
200201

202+
msg <- paste0(
203+
if (!is_simple_error(x)) paste0("<", paste(class(x), collapse = "/"), ">\n"),
204+
header, cnd_message(x)
205+
)
206+
201207
expectation("error", msg, srcref, trace = x[["trace"]])
202208
}
203209

210+
is_simple_error <- function(x) {
211+
class(x)[[1]] %in% c("simpleError", "rlang_error")
212+
}
213+
204214
#' @export
205215
as.expectation.warning <- function(x, srcref = NULL) {
206216
expectation("warning", cnd_message(x), srcref, trace = x[["trace"]])

tests/testthat/_snaps/reporter-progress.md

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
8. signaller() reporters/backtraces.R:32:7
204204

205205
Error (backtraces.R:43:3): Errors are inspected with `conditionMessage()`
206+
<foobar/rlang_error/error/condition>
206207
Error: dispatched
207208

208209
Warning (backtraces.R:50:3): also get backtraces for warnings

tests/testthat/test-expectation.R

+2-17
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,14 @@ test_that("conditionMessage() is called during conversion", {
3535
expect_identical(as.expectation(wrn)$message, "dispatched")
3636

3737
err <- error_cnd("foobar", message = "wrong")
38-
expect_identical(as.expectation(err)$message, "Error: dispatched")
38+
expect_match(as.expectation(err)$message, "Error: dispatched")
3939

4040
err <- cnd(c("foobar", "skip"), message = "wrong")
4141
expect_identical(as.expectation(err)$message, "dispatched")
4242
})
4343

4444
test_that("error message includes call", {
45-
4645
f <- function() stop("Error!")
4746
cnd <- catch_cnd(f())
48-
exp <- as.expectation(cnd)
49-
50-
local_bindings(
51-
conditionMessage.foobar = function(...) "dispatched",
52-
.env = global_env()
53-
)
54-
55-
wrn <- warning_cnd("foobar", message = "wrong")
56-
expect_identical(as.expectation(wrn)$message, "dispatched")
57-
58-
err <- error_cnd("foobar", message = "wrong")
59-
expect_identical(as.expectation(err)$message, "Error: dispatched")
60-
61-
err <- cnd(c("foobar", "skip"), message = "wrong")
62-
expect_identical(as.expectation(err)$message, "dispatched")
47+
expect_equal(format(as.expectation(cnd)), "Error in `f()`: Error!")
6348
})

0 commit comments

Comments
 (0)