# A custom error with base
f <- function() {
message <- paste(c("Oh no", "Messed up", "Need a hint?"), collapse = "\n")
# Construct a custom class via `errorCondition()`
stop(errorCondition(message, class = "my_error"))
}
# How it prints
f()
#> Error: Oh no
#> Messed up
#> Need a hint?
# How to test it
testthat::expect_error(tools::assertError(f(), classes = "my_error"), NA)
# A custom error with rlang
f <- function() {
# `x`, `i`, and unnamed bullets print nicely via `rlang::format_error_bullets()`
message <- c("Oh no", x = "Messed up", i = "Need a hint?")
# Construct a custom class direcly via `rlang::abort()`
rlang::abort(message, class = "my_error")
}
# How it prints
f()
#> Error: Oh no
#> x Messed up
#> ℹ Need a hint?
# How to test it
testthat::expect_error(f(), class = "my_error")
Created on 2021-08-27 by the reprex package (v2.0.1)