diff --git a/R/era.R b/R/era.R index 95c6aeb..de4a63c 100644 --- a/R/era.R +++ b/R/era.R @@ -161,7 +161,8 @@ validate_era <- function(x) { # scale # direction if (!era_direction(x) %in% c(-1, 1)) { - stop("direction must be -1 (backwards) or 1 (forwards)") + abort("`direction` must be -1 (backwards) or 1 (forwards)", + class = "era_invalid_era") } } diff --git a/R/transform.R b/R/transform.R index 4a82322..537e6c6 100644 --- a/R/transform.R +++ b/R/transform.R @@ -40,8 +40,14 @@ yr_transform <- function(x, era = yr_era(x)) { # Check comparable units if (era_unit(src_era) != era_unit(dst_era)) { - stop("No method for transforming ", era_unit(src_era), " to ", - era_unit(dst_era), " years") + abort( + paste0("Can't transform era ", era_label(src_era), " to ", era_label(dst_era), ":"), + class = "era_invalid_transform", + body = format_error_bullets(c( + x = paste0("Can't convert ", era_unit(src_era), " to ", + era_unit(dst_era), " years.") + )) + ) } # Rescale to 1 (if not already) diff --git a/R/yr.R b/R/yr.R index acd60b5..68ce225 100644 --- a/R/yr.R +++ b/R/yr.R @@ -37,7 +37,8 @@ yr <- function(x = numeric(), era) { x <- vec_cast(x, numeric()) if (vec_size(era) > 1) { - stop("yr vectors can only have one era attribute") + abort("yr vectors must have only one `era` attribute.", + class = "era_invalid_yr") } if (is.character(era)) { era <- era(era) diff --git a/tests/testthat/test-transform.R b/tests/testthat/test-transform.R index c016cb8..4b7d3f7 100644 --- a/tests/testthat/test-transform.R +++ b/tests/testthat/test-transform.R @@ -1,4 +1,4 @@ test_that("yr_transform() throws an error for different units", { expect_error(yr_transform(yr(9000, "bp"), era("BP")), - "No method for transforming radiocarbon to calendar years") + class = "era_invalid_transform") }) diff --git a/tests/testthat/test-yr.R b/tests/testthat/test-yr.R index f477a37..25fc40e 100644 --- a/tests/testthat/test-yr.R +++ b/tests/testthat/test-yr.R @@ -5,8 +5,7 @@ test_that("yr() constructs an era_yr with valid input", { }) test_that("yr() throws an error with multiple eras", { - expect_error(yr(1, era(c("BC", "BP"))), - "yr vectors can only have one era attribute") + expect_error(yr(1, era(c("BC", "BP"))), class = "era_invalid_yr") }) test_that("format.yr returns correct output", {