Skip to content

Commit

Permalink
Enable searches for multiple warnings. (#9)
Browse files Browse the repository at this point in the history
* Enable searches for multiple warnings. (Close #7)

* Roll new version
  • Loading branch information
coatless committed Feb 22, 2020
1 parent b86c4c7 commit d79f271
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: errorist
Title: Automatically Search Errors or Warnings
Version: 0.0.3.9000
Version: 0.1.0
Authors@R: c(
person("James", "Balamuta",
email = "balamut2@illinois.edu",
Expand Down
13 changes: 11 additions & 2 deletions NEWS.md
@@ -1,10 +1,19 @@
# errorist 0.0.3.9000
# errorist 0.1.0

## Feature

- When multiple warnings occur, each unique warning is searched separately
instead of searching only the latest value. ([#9](https://github.com/r-assist/errorist/pull/9),
closing ([#8](https://github.com/r-assist/errorist/issues/7)))

## Fixes

- Warning messages now have their locale specific prefix.
([#9](https://github.com/r-assist/errorist/pull/9))

- Ensured the `last.warning` object was created by setting `options('warn' = 0)`,
which is the default value. Upon unload, the warning level is restored to
the old value.
the old value. ([#8](https://github.com/r-assist/errorist/pull/8))

# errorist 0.0.3

Expand Down
54 changes: 46 additions & 8 deletions R/handlers.R
Expand Up @@ -59,6 +59,43 @@ disable_errorist = function() {
}


warning_msg_format = function(x = warnings()) {
# Retrieve the error message
warning_messages = names(x)
# Retrieve the call location for the error
# E.g. fun(1, 2) : this is a warning
warning_calls = as.character(x)

# Set default sep value
sep_val = ": "

# Build a traditional warning string
combined_warning = paste(warning_calls, warning_messages, sep = sep_val)

# Format string by removing instances of NULL
missing_call = which(warning_calls == "NULL")
if (length(missing_call)) {
# Compute number of characters to drop
call_drop = nchar(paste0("NULL", sep_val)) + 1L

# Remove NULL in string.
combined_warning[missing_call] =
substr(combined_warning[missing_call],
call_drop,
nchar(combined_warning[missing_call]))
}

# Determine number of warnings
n = length(combined_warning)

# Ensure a localized warning is given.
translated_warning_prefix = ngettext(n, "Warning message:\n", "Warning messages:\n")

# Return fixed warning string
paste0(translated_warning_prefix, combined_warning)
}


warning_handler = function(search_func =
getOption("errorist.warning", searcher::search_google)) {
# Write trigger as a closure with the required four inputs
Expand All @@ -74,22 +111,23 @@ warning_handler = function(search_func =
# see https://developer.r-project.org/TaskHandlers.pdf
function(expr, value, ok, visible) {
# Determines if a warning was triggered
last_warning_value = get0("last.warning", envir = baseenv())
last_warning_value = warnings()

if (!is.null(last_warning_value)) {
warning_contents = names(last_warning_value)[1]
warning_contents = warning_msg_format(last_warning_value)
} else {
warning_contents = NA
warning_contents = NA
}

# Trigger search with added protections since this caller is repeatedly
# called regardless if a new warning is detected.
if (!is.na(warning_contents) &&
(
is.na(.errorist_env$captured_last_search_warning) ||
.errorist_env$captured_last_search_warning != warning_contents
)) {
search_func(warning_contents)
(is.na(.errorist_env$captured_last_search_warning) ||
.errorist_env$captured_last_search_warning != warning_contents)
) {
for (warning_msg in warning_contents) {
search_func(warning_msg)
}
}

# Update last warning
Expand Down

0 comments on commit d79f271

Please sign in to comment.