Skip to content

Commit 2c66dce

Browse files
committed
better warnings with proper condition classes when logging encounteres an error
1 parent cc8083a commit 2c66dce

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

R/Logger.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,13 @@ Logger <- R6::R6Class(
276276
){
277277
get("append", envir = app)(event)
278278
}
279-
}, error = get("handle_exception", envir = self))
280-
}
279+
}, error = function(e) {
280+
e$appender <- app
281+
get("handle_exception", envir = self)(e)
282+
}
283+
)
281284
}
285+
}
282286

283287
invisible(msg)
284288
},
@@ -916,11 +920,13 @@ LoggerGlue <- R6::R6Class(
916920
){
917921
get("append", envir = app)(event)
918922
}
919-
},
920-
error = get("handle_exception", envir = self)
921-
)
922-
}
923+
}, error = function(e) {
924+
e$appender <- app
925+
get("handle_exception", envir = self)(e)
926+
}
927+
)
923928
}
929+
}
924930
invisible(msg)
925931
},
926932
error = get("handle_exception", envir = self)

R/default_functions.R

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,40 @@
1414
#' tryCatch(stop("an error has occurred"), error = default_exception_handler)
1515
#'
1616
default_exception_handler <- function(e){
17-
warning(
18-
"[", format(Sys.time(), format = "%Y-%m-%d %H:%M:%OS3"), "] ",
19-
"An error occurred during logging: ", e, call. = FALSE
17+
ts <- paste0("[", format(Sys.time(), format = "%Y-%m-%d %H:%M:%OS3"), "]")
18+
19+
if ("appender" %in% names(e)){
20+
res <- appendingFailedWarning(
21+
paste(ts, paste(class_fmt(e$appender, ignore = c("Filterable", "R6", "Appender")), "encountered an error:", e$message)),
22+
error = e
23+
)
24+
} else {
25+
res <- loggingFailedWarning(
26+
paste(ts, "an error occurred during logging:", e$message),
27+
error = e
28+
)
29+
}
30+
warning(res)
31+
}
32+
33+
34+
35+
appendingFailedWarning <- function(message, class = NULL, call = NULL, ...){
36+
warning_condition(
37+
message = message,
38+
class = union(class, "appendingFailedWarning"),
39+
call = call,
40+
...
41+
)
42+
}
43+
44+
45+
46+
loggingFailedWarning <- function(message, class = NULL, call = NULL, ...){
47+
warning_condition(
48+
message = message,
49+
class = union(class, "loggingFailedWarning"),
50+
call = call,
51+
...
2052
)
2153
}

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,14 @@ error <- function(message, class, call = NULL, ...) {
269269
list(message = message, call = call, ...)
270270
)
271271
}
272+
273+
274+
275+
276+
warning_condition <- function(message, class, call = NULL, ...) {
277+
structure(
278+
class = union(class, c("warning", "condition")),
279+
list(message = message, call = call, ...)
280+
)
281+
}
272282
# nocov end

man/LayoutJson.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_Logger.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ test_that("Logger$log() dispatches to all appenders, even if some throw an error
387387
file = AppenderFile$new(file = tf)
388388
))
389389

390-
expect_warning(ln$info("test_normal"))
391-
expect_warning(ln$info("test_glue"))
390+
expect_warning(ln$info("test_normal"), class = "appendingFailedWarning")
391+
expect_warning(ln$info("test_glue"), class = "appendingFailedWarning")
392392

393393
expect_true(any(grepl("test_normal", readLines(tf))))
394394
expect_true(any(grepl("test_glue", readLines(tf))))

0 commit comments

Comments
 (0)