Skip to content

Commit d79f271

Browse files
authored
Enable searches for multiple warnings. (#9)
* Enable searches for multiple warnings. (Close #7) * Roll new version
1 parent b86c4c7 commit d79f271

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: errorist
22
Title: Automatically Search Errors or Warnings
3-
Version: 0.0.3.9000
3+
Version: 0.1.0
44
Authors@R: c(
55
person("James", "Balamuta",
66
email = "[email protected]",

NEWS.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
# errorist 0.0.3.9000
1+
# errorist 0.1.0
2+
3+
## Feature
4+
5+
- When multiple warnings occur, each unique warning is searched separately
6+
instead of searching only the latest value. ([#9](https://github.com/r-assist/errorist/pull/9),
7+
closing ([#8](https://github.com/r-assist/errorist/issues/7)))
28

39
## Fixes
410

11+
- Warning messages now have their locale specific prefix.
12+
([#9](https://github.com/r-assist/errorist/pull/9))
13+
514
- Ensured the `last.warning` object was created by setting `options('warn' = 0)`,
615
which is the default value. Upon unload, the warning level is restored to
7-
the old value.
16+
the old value. ([#8](https://github.com/r-assist/errorist/pull/8))
817

918
# errorist 0.0.3
1019

R/handlers.R

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,43 @@ disable_errorist = function() {
5959
}
6060

6161

62+
warning_msg_format = function(x = warnings()) {
63+
# Retrieve the error message
64+
warning_messages = names(x)
65+
# Retrieve the call location for the error
66+
# E.g. fun(1, 2) : this is a warning
67+
warning_calls = as.character(x)
68+
69+
# Set default sep value
70+
sep_val = ": "
71+
72+
# Build a traditional warning string
73+
combined_warning = paste(warning_calls, warning_messages, sep = sep_val)
74+
75+
# Format string by removing instances of NULL
76+
missing_call = which(warning_calls == "NULL")
77+
if (length(missing_call)) {
78+
# Compute number of characters to drop
79+
call_drop = nchar(paste0("NULL", sep_val)) + 1L
80+
81+
# Remove NULL in string.
82+
combined_warning[missing_call] =
83+
substr(combined_warning[missing_call],
84+
call_drop,
85+
nchar(combined_warning[missing_call]))
86+
}
87+
88+
# Determine number of warnings
89+
n = length(combined_warning)
90+
91+
# Ensure a localized warning is given.
92+
translated_warning_prefix = ngettext(n, "Warning message:\n", "Warning messages:\n")
93+
94+
# Return fixed warning string
95+
paste0(translated_warning_prefix, combined_warning)
96+
}
97+
98+
6299
warning_handler = function(search_func =
63100
getOption("errorist.warning", searcher::search_google)) {
64101
# Write trigger as a closure with the required four inputs
@@ -74,22 +111,23 @@ warning_handler = function(search_func =
74111
# see https://developer.r-project.org/TaskHandlers.pdf
75112
function(expr, value, ok, visible) {
76113
# Determines if a warning was triggered
77-
last_warning_value = get0("last.warning", envir = baseenv())
114+
last_warning_value = warnings()
78115

79116
if (!is.null(last_warning_value)) {
80-
warning_contents = names(last_warning_value)[1]
117+
warning_contents = warning_msg_format(last_warning_value)
81118
} else {
82-
warning_contents = NA
119+
warning_contents = NA
83120
}
84121

85122
# Trigger search with added protections since this caller is repeatedly
86123
# called regardless if a new warning is detected.
87124
if (!is.na(warning_contents) &&
88-
(
89-
is.na(.errorist_env$captured_last_search_warning) ||
90-
.errorist_env$captured_last_search_warning != warning_contents
91-
)) {
92-
search_func(warning_contents)
125+
(is.na(.errorist_env$captured_last_search_warning) ||
126+
.errorist_env$captured_last_search_warning != warning_contents)
127+
) {
128+
for (warning_msg in warning_contents) {
129+
search_func(warning_msg)
130+
}
93131
}
94132

95133
# Update last warning

0 commit comments

Comments
 (0)