Skip to content

Commit

Permalink
Switch to rlang condition handling. Close #26
Browse files Browse the repository at this point in the history
  • Loading branch information
joeroe committed Nov 20, 2020
1 parent b844c2a commit cf959e1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion R/era.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
10 changes: 8 additions & 2 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/yr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-transform.R
Original file line number Diff line number Diff line change
@@ -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")
})
3 changes: 1 addition & 2 deletions tests/testthat/test-yr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit cf959e1

Please sign in to comment.