Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions R/downloadSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ downloadSource <- function(type, subtype = NULL, overwrite = FALSE, numberOfTrie
with_dir(downloadInProgressDirectory, {
setWrapperActive("wrapperChecks")
# convert warnings to errors, because in download functions warnings usually mean data was not downloaded properly
withr::with_options(c(warn = 2), {
meta <- withMadratLogging(eval(parse(text = functionCall)), logOnly = FALSE)
})
meta <- withMadratLogging(eval(parse(text = functionCall)), logOnly = FALSE, warningsAsErrors = TRUE)
setWrapperInactive("wrapperChecks")

# define mandatory elements of meta data and check if they exist
Expand Down
22 changes: 18 additions & 4 deletions R/withMadratLogging.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#'
#'
#' @param expr expression to be evaluated.
#' @param logOnly passed to vcat, determines if warning/error is thrown after logging
#' @param logOnly passed to vcat, determines if warning/error is thrown after logging.
#' If omitted in a nested call, the enclosing logging policy is inherited.
#' @param warningsAsErrors whether warnings should be logged and handled as errors.
#' If omitted in a nested call, the enclosing logging policy is inherited.
#' @author Jan Philipp Dietrich
#' @seealso \code{\link{vcat}}
#' @keywords internal
Expand All @@ -15,7 +18,15 @@
#' madrat:::withMadratLogging(message("Hello world!"))
#' }
#'
withMadratLogging <- function(expr, logOnly = TRUE) {
withMadratLogging <- function(expr, logOnly = TRUE, warningsAsErrors = FALSE) {
loggingPolicy <- getOption(
"madrat_loggingPolicy",
list(logOnly = TRUE, warningsAsErrors = FALSE)
)
if (!missing(logOnly)) loggingPolicy$logOnly <- logOnly
if (!missing(warningsAsErrors)) loggingPolicy$warningsAsErrors <- warningsAsErrors
withr::local_options(madrat_loggingPolicy = loggingPolicy)

if (isWrapperActive("callingHandler")) {
return(expr)
}
Expand All @@ -27,11 +38,14 @@ withMadratLogging <- function(expr, logOnly = TRUE) {
}

warningHandler <- function(w) {
vcat(0, w$message, logOnly = logOnly)
loggingPolicy <- getOption("madrat_loggingPolicy")
verbosity <- ifelse(loggingPolicy$warningsAsErrors, -1, 0)
vcat(verbosity, w$message, logOnly = loggingPolicy$logOnly)
}

errorHandler <- function(w) {
vcat(-1, w$message, logOnly = logOnly)
loggingPolicy <- getOption("madrat_loggingPolicy")
vcat(-1, w$message, logOnly = loggingPolicy$logOnly)
}

setWrapperActive("callingHandler")
Expand Down
8 changes: 6 additions & 2 deletions man/withMadratLogging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/testthat/test-vcat-withMadratLogging.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,42 @@ test_that("withMadratLogging properly logs warnings", {
}
})

test_that("withMadratLogging can treat warnings as errors", {
warningMessage <- "This warning is an error"
expect_error(
expect_message(
withMadratLogging(warning(warningMessage), logOnly = FALSE, warningsAsErrors = TRUE),
paste0("^ERROR: ", warningMessage, "\n$")
),
warningMessage,
fixed = TRUE
)
})

test_that("nested withMadratLogging calls inherit and can override policies", {
inheritedWarning <- "This warning inherits the outer policy"
expect_error(
withMadratLogging(
withMadratLogging(warning(inheritedWarning)),
logOnly = FALSE,
warningsAsErrors = TRUE
),
inheritedWarning,
fixed = TRUE
)

overriddenWarning <- "This warning overrides the outer policy"
expect_warning(
withMadratLogging(
withMadratLogging(warning(overriddenWarning), logOnly = TRUE, warningsAsErrors = FALSE),
logOnly = FALSE,
warningsAsErrors = TRUE
),
overriddenWarning,
fixed = TRUE
)
})

test_that("withMadratLogging properly logs messages", {
m <- "This is a message"
mExp <- paste0("^NOTE: ", m, "\n$")
Expand Down
Loading