-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgtfsio_error.R
53 lines (43 loc) · 1.59 KB
/
gtfsio_error.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#' gtfsio's custom error condition constructor
#'
#' @param message The message to inform about the error.
#' @param subclass The subclass of the error.
#' @param call A call to associate the error with.
#'
#' @family error constructors
#'
#' @keywords internal
gtfsio_error <- function(message,
subclass = character(0),
call = sys.call(-1)) {
# input checking
if (! (is.character(message) && length(message) == 1))
stop("'message' must be a string.")
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 <- utils::tail(as.character(call[[1]]), 1)
fn_error_class <- paste0(fn_name, "_error")
subclass <- c(subclass, fn_error_class, "gtfsio_error")
error <- errorCondition(message, class = subclass, call = NULL)
stop(error)
}
#' Parent error function constructor
#'
#' Creates a function that raises an error that is assigned to the function in
#' which the error was originally seen. Useful to prevent big repetitive
#' `gtfsio_error()` calls in the "main" functions.
#'
#' @param message The message to inform about the error.
#' @param subclass The subclass of the error.
#'
#' @family error constructors
#'
#' @keywords internal
parent_function_error <- function(message, subclass = character(0)) {
function() {
parent_call <- sys.call(-1)
gtfsio_error(message, subclass, parent_call)
}
}