Skip to content

Commit

Permalink
custom geocodebr error
Browse files Browse the repository at this point in the history
  • Loading branch information
dhersz committed Dec 11, 2024
1 parent 85d1e8e commit eb23bb3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions R/error.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
geocodebr_error <- function(message, call, .envir = parent.frame()) {
error_call <- sys.call(-1)
error_function <- as.name(error_call[[1]])

error_classes <- c(
paste0("geocodebr_error_", sub("^error_", "", error_function)),
"geocodebr_error"
)

cli::cli_abort(message, class = error_classes, call = call, .envir = .envir)
}
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# erro funciona corretamente

Code
parent_function()
Condition <geocodebr_error_test>
Error in `parent_function()`:
! test
* info

12 changes: 12 additions & 0 deletions tests/testthat/test-error.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parent_function <- function() error_test()

error_test <- function() {
geocodebr_error(c("test", "*" = "info"), call = rlang::caller_env())
}

test_that("erro funciona corretamente", {
expect_error(parent_function(), class = "geocodebr_error_test")
expect_error(parent_function(), class = "geocodebr_error")

expect_snapshot(parent_function(), error = TRUE, cnd_class = TRUE)
})

0 comments on commit eb23bb3

Please sign in to comment.