Skip to content

Commit

Permalink
small fix to gtfsio_error() and corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
dhersz committed Dec 23, 2021
1 parent 3a965bc commit 753dfa1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion R/gtfsio_error.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ gtfsio_error <- function(message,
if (! is.character(subclass)) stop("'subclass' must be a character vector.")

# retrieve the function that called 'gtfsio_error' and include it as a class
# the tail() call ensures that we pick the name of the function in case of a
# namespaced call (e.g. gtfsio::assert_file_exists())

fn_name <- as.character(call[[1]])
fn_name <- utils::tail(as.character(call[[1]]), 1)
fn_error_class <- paste0(fn_name, "_error")

subclass <- c(subclass, fn_error_class, "gtfsio_error")
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"sameAs": "https://CRAN.R-project.org/package=zip"
}
],
"fileSize": "104491.608KB",
"fileSize": "104492.514KB",
"keywords": [
"r",
"gtfs"
Expand Down
15 changes: 15 additions & 0 deletions inst/tinytest/test_gtfsio_error.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ last_error <- tryCatch(
)
expect_inherits(last_error, "function_to_assign_error")

# error resulting from a gtfsio function called via the namespace operator (::)
# (e.g. gtfsio::assert_file_exists()) should assign the error to the function,
# but not to :: and gtfsio (:: is a function that takes the package and the
# function as arguments - if we don't pay attention to this, gtfsio_error() will
# assign the error to the wrong function in such cases)

path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(path)
namespaced_error <- tryCatch(
gtfsio::assert_file_exists(gtfs, "oi"),
error = function(cnd) cnd
)
expect_inherits(namespaced_error, "assert_file_exists_error")
expect_true(sum(grepl("gtfsio_error", class(namespaced_error))) == 1)
expect_false(inherits(namespaced_error, "::_error"))

# parent_function_error() -------------------------------------------------

Expand Down

0 comments on commit 753dfa1

Please sign in to comment.