Skip to content

Commit

Permalink
feat: Get msg via 'conditionMessage'
Browse files Browse the repository at this point in the history
Use generic functions where possible
  • Loading branch information
MEO265 committed Jul 23, 2024
1 parent 4bc1bd7 commit 69c0255
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ message <- function(..., domain = NULL, appendLF = TRUE, .loggit = NA, echo = ge
base::message(..1)
}, message = function(m) {
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 3L)) {
loggit_internal(log_lvl = "INFO", log_msg = m[["message"]], echo = echo)
loggit_internal(log_lvl = "INFO", log_msg = conditionMessage(m), echo = echo)
}
# If signalCondition was used there would be no output to the console
base::message(m)
Expand All @@ -41,9 +41,9 @@ message <- function(..., domain = NULL, appendLF = TRUE, .loggit = NA, echo = ge
tryCatch({
base::message(..., domain = domain, appendLF = appendLF)
}, message = function(m) {
m <- simpleMessage(message = m[["message"]], call = call)
m <- simpleMessage(message = conditionMessage(m), call = call)
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 3L)) {
loggit_internal(log_lvl = "INFO", log_msg = m[["message"]], echo = echo)
loggit_internal(log_lvl = "INFO", log_msg = conditionMessage(m), echo = echo)
}
# If signalCondition was used there would be no output to the console
base::message(m)
Expand Down Expand Up @@ -83,7 +83,7 @@ warning <- function(..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE,
base::warning(..1)
}, warning = function(w) {
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 2L)) {
loggit_internal(log_lvl = "WARN", log_msg = w[["message"]], echo = echo)
loggit_internal(log_lvl = "WARN", log_msg = conditionMessage(w), echo = echo)
}
# If signalCondition was used there would be no output to the console
base::warning(w)
Expand All @@ -92,9 +92,9 @@ warning <- function(..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE,
tryCatch({
base::warning(..., call. = FALSE, immediate. = immediate., noBreaks. = noBreaks., domain = domain)
}, warning = function(w) {
w <- simpleWarning(message = w[["message"]], call = call)
w <- simpleWarning(message = conditionMessage(w), call = call)
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 2L)) {
loggit_internal(log_lvl = "WARN", log_msg = w[["message"]], echo = echo)
loggit_internal(log_lvl = "WARN", log_msg = conditionMessage(w), echo = echo)
}
# If signalCondition was used there would be no output to the console
base::warning(w)
Expand Down Expand Up @@ -134,17 +134,17 @@ stop <- function(..., call. = TRUE, domain = NULL, .loggit = NA, echo = get_echo
base::stop(..1)
}, error = function(e) {
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 1L)) {
loggit_internal(log_lvl = "ERROR", log_msg = e[["message"]], echo = echo)
loggit_internal(log_lvl = "ERROR", log_msg = conditionMessage(e), echo = echo)
}
base::stop(e)
})
} else {
tryCatch({
base::stop(..., call. = FALSE, domain = domain)
}, error = function(e) {
e <- simpleError(message = e[["message"]], call = call)
e <- simpleError(message = conditionMessage(e), call = call)
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 1L)) {
loggit_internal(log_lvl = "ERROR", log_msg = e[["message"]], echo = echo)
loggit_internal(log_lvl = "ERROR", log_msg = conditionMessage(e), echo = echo)
}
signalCondition(e)
})
Expand Down Expand Up @@ -194,9 +194,9 @@ stopifnot <- function(..., exprs, exprObject, local, .loggit = NA, echo = get_ec
tryCatch({
eval.parent(stop_call, 1L)
}, error = function(e) {
cond <- simpleError(message = e[["message"]], call = call)
cond <- simpleError(message = conditionMessage(e), call = call)
if (isTRUE(.loggit) || (!isFALSE(.loggit) && get_log_level() >= 1L)) {
loggit_internal(log_lvl = "ERROR", log_msg = cond[["message"]], echo = echo)
loggit_internal(log_lvl = "ERROR", log_msg = conditionMessage(cond), echo = echo)
}
signalCondition(cond = cond)
})
Expand Down
6 changes: 3 additions & 3 deletions R/whit_loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ with_loggit <- function(exp, logfile = get_logfile(), echo = get_echo(), log_lev
withCallingHandlers(
exp,
error = function(e) {
if (log_error) loggit_internal(log_lvl = "ERROR", log_msg = e[["message"]], echo = echo, logfile = log_con)
if (log_error) loggit_internal(log_lvl = "ERROR", log_msg = conditionMessage(e), echo = echo, logfile = log_con)
},
warning = function(w) {
if (log_warn) loggit_internal(log_lvl = "WARN", log_msg = w[["message"]], echo = echo, logfile = log_con)
if (log_warn) loggit_internal(log_lvl = "WARN", log_msg = conditionMessage(w), echo = echo, logfile = log_con)
},
message = function(m) {
if (log_info) loggit_internal(log_lvl = "INFO", log_msg = m[["message"]], echo = echo, logfile = log_con)
if (log_info) loggit_internal(log_lvl = "INFO", log_msg = conditionMessage(m), echo = echo, logfile = log_con)
}
)
}
4 changes: 2 additions & 2 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ expect_identical_condition <-
testthat::fail("Expected don't throws an error.")
}

if (actual[["message"]] != expected[["message"]]) {
if (conditionMessage(actual) != conditionMessage(expected)) {
testthat::fail(
sprintf("Actual message is '%s' and expected is '%s'.", actual[["message"]], expected[["message"]])
sprintf("Actual message is '%s' and expected is '%s'.", conditionMessage(actual), conditionMessage(expected))
)
}

Expand Down

0 comments on commit 69c0255

Please sign in to comment.