-
Notifications
You must be signed in to change notification settings - Fork 6
Untracked: Rethrow UserErrors in exceptions #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ded1aa3
Untracked: Rethrow UserErrors in exceptions
jrwishart 0e2fdf9
fixup! Untracked: Rethrow UserErrors in exceptions
jrwishart 038afdc
Version bump
jrwishart 524e83c
Some linting changes
jrwishart 578ea37
Add .github to .Rbuildignore
jrwishart 63ffcf7
Add missing brace
jrwishart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| ^\.Rproj\.user$ | ||
| .*~ | ||
| ^\.circleci/config\.yml$ | ||
| ^\.github/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,18 +12,22 @@ readDataSets <- function(data.set.names, min.data.sets = 1) | |
| data.set.names <- vapply(data.set.names, trimws, character(1), | ||
| USE.NAMES = FALSE) | ||
|
|
||
| if (length(data.set.names) < min.data.sets) | ||
| if (length(data.set.names) < min.data.sets) { | ||
| StopForUserError("At least ", min.data.sets, " data set(s) are required.") | ||
| } | ||
|
|
||
| if (!all(grepl('.+\\.sav$', data.set.names, ignore.case = TRUE))) { | ||
| StopForUserError("An input data file was not an SPSS .sav data file. ", | ||
| "Only SPSS .sav data files are accepted.") | ||
| if (!all(grepl(".+\\.sav$", data.set.names, ignore.case = TRUE))) { | ||
| StopForUserError( | ||
| "An input data file was not an SPSS .sav data file. ", | ||
| "Only SPSS .sav data files are accepted." | ||
| ) | ||
| } | ||
|
|
||
| if (IsDisplayrCloudDriveAvailable()) | ||
| if (IsDisplayrCloudDriveAvailable()) { | ||
| readDataSetsFromDisplayrCloudDrive(data.set.names) | ||
| else | ||
| } else { | ||
| readLocalDataSets(data.set.names) | ||
| } | ||
| } | ||
|
|
||
| #' @param data.set.paths A character vector of paths to local data files. | ||
|
|
@@ -34,7 +38,7 @@ readDataSets <- function(data.set.names, min.data.sets = 1) | |
| readLocalDataSets <- function(data.set.paths, parser = read_sav) | ||
| { | ||
| result <- lapply(data.set.paths, function(path) { | ||
| handler = createReadErrorHandler(path) | ||
| handler <- createReadErrorHandler(path) | ||
| InterceptExceptions(parser(path), error.handler = handler) | ||
| }) | ||
| names(result) <- basename(data.set.paths) | ||
|
|
@@ -49,43 +53,57 @@ readLocalDataSets <- function(data.set.paths, parser = read_sav) | |
| readDataSetsFromDisplayrCloudDrive <- function(data.set.names) | ||
| { | ||
| result <- lapply(data.set.names, function(nm) { | ||
| handler = createReadErrorHandler(nm) | ||
| handler <- createReadErrorHandler(nm) | ||
| InterceptExceptions(QLoadData(nm), error.handler = handler) | ||
| }) | ||
| names(result) <- data.set.names | ||
| result | ||
| } | ||
|
|
||
| createExceptionHandler <- function(intercept.messages, | ||
| replacement.messages, warn = FALSE) | ||
| { | ||
| if (length(replacement.messages) == 1 && length(intercept.messages) > 1) | ||
| #' @importFrom flipU StopForUserError | ||
| createExceptionHandler <- function( | ||
| intercept.messages, | ||
| replacement.messages, | ||
| warn = FALSE | ||
| ) { | ||
| if (length(replacement.messages) == 1 && length(intercept.messages) > 1) { | ||
| replacement.messages <- rep(replacement.messages, length(intercept.messages)) | ||
| condition.fun <- if (warn) warning else stop | ||
| function(e) | ||
| { | ||
| } | ||
| function(e) { | ||
| condition.fun <- function(...) { | ||
| if (warn) { | ||
| warning(..., call. = FALSE) | ||
| } else if (inherits(e, "UserError")) { | ||
| StopForUserError(...) | ||
| } else { | ||
| stop(..., call. = FALSE) | ||
| } | ||
| } | ||
|
Comment on lines
+73
to
+81
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual change. Some minor differences below since the |
||
| msg.found <- FALSE | ||
| for (i in seq_along(intercept.messages)) | ||
| { | ||
| if (grepl(intercept.messages[i], e$message)) | ||
| { | ||
| condition.fun(replacement.messages[i], call. = FALSE) | ||
| for (i in seq_along(intercept.messages)) { | ||
| if (grepl(intercept.messages[i], e$message)) { | ||
| condition.fun(replacement.messages[i]) | ||
| msg.found <- TRUE | ||
| } | ||
| } | ||
| if (!msg.found) | ||
| condition.fun(e$message, call. = FALSE) | ||
| if (!msg.found) { | ||
| condition.fun(e$message) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| createReadErrorHandler <- function(data.set.name) | ||
| { | ||
| replacement.msg <- paste0("The data file '", data.set.name, "' could not be parsed. ", | ||
| "The data file may be fixed by inserting it in a Displayr document, ", | ||
| "exporting it as an SPSS file (.sav) via the Publish button, ", | ||
| "and then uploading it back to the cloud drive.") | ||
| intercept.msgs <- c("Invalid file, or file has unsupported features", | ||
| "Unable to convert string to the requested encoding") | ||
| replacement.msg <- paste0( | ||
| "The data file '", data.set.name, "' could not be parsed. ", | ||
| "The data file may be fixed by inserting it in a Displayr document, ", | ||
| "exporting it as an SPSS file (.sav) via the Publish button, ", | ||
| "and then uploading it back to the cloud drive." | ||
| ) | ||
| intercept.msgs <- c( | ||
| "Invalid file, or file has unsupported features", | ||
| "Unable to convert string to the requested encoding" | ||
| ) | ||
| createExceptionHandler(intercept.msgs, replacement.msg, warn = FALSE) | ||
| } | ||
|
|
||
|
|
@@ -97,24 +115,24 @@ createReadErrorHandler <- function(data.set.name) | |
| #' @importFrom flipAPI QSaveData IsDisplayrCloudDriveAvailable | ||
| #' @importFrom flipU InterceptExceptions | ||
| #' @importFrom tools file_path_sans_ext | ||
| writeDataSet <- function(data.set, data.set.name, is.saved.to.cloud) | ||
| { | ||
| if (is.saved.to.cloud) | ||
| { | ||
| writeDataSet <- function(data.set, data.set.name, is.saved.to.cloud) { | ||
| if (is.saved.to.cloud) { | ||
| warn.msg <- paste0("The data file ", data.set.name, | ||
| " has been compressed into ", file_path_sans_ext(data.set.name), | ||
| ".zip on the Cloud Drive as it is too large. ", | ||
| "It needs to be downloaded, unzipped and re-uploaded to be ", | ||
| "used in a Displayr document.") | ||
| error.msg <- paste0("The data file could not be saved due to invalid characters ", | ||
| "in some of the variable names. Please contact support for assistance.") | ||
| InterceptExceptions(QSaveData(data.set, data.set.name, 2e9), # 2e9 bytes seems to be just below the API upload limit for the cloud drive | ||
| warning.handler = createExceptionHandler("Object compressed into a zip file", | ||
| warn.msg, TRUE), | ||
| error.handler = createExceptionHandler("must have valid SPSS variable names", | ||
| error.msg, FALSE)) | ||
| }else | ||
| # 2e9 bytes seems to be just below the API upload limit for the cloud drive | ||
| InterceptExceptions( | ||
| QSaveData(data.set, data.set.name, 2e9), | ||
| warning.handler = createExceptionHandler("Object compressed into a zip file", warn.msg, TRUE), | ||
| error.handler = createExceptionHandler("must have valid SPSS variable names", error.msg, FALSE) | ||
| ) | ||
| } else { | ||
| write_sav(data.set, data.set.name) | ||
| } | ||
| } | ||
|
|
||
| #' @description Creates a list of metadata for a data set | ||
|
|
@@ -239,12 +257,12 @@ variableType <- function(variable) | |
| StopForUserError("Variable type not recognised") | ||
| } | ||
|
|
||
| NUMERIC.VARIABLE.TYPE = "Numeric"; | ||
| TEXT.VARIABLE.TYPE = "Text"; | ||
| CATEGORICAL.VARIABLE.TYPE = "Categorical"; | ||
| DATE.VARIABLE.TYPE = "Date"; | ||
| DATE.TIME.VARIABLE.TYPE = "Date/Time"; | ||
| DURATION.VARIABLE.TYPE = "Duration"; | ||
| NUMERIC.VARIABLE.TYPE <- "Numeric" | ||
| TEXT.VARIABLE.TYPE <- "Text" | ||
| CATEGORICAL.VARIABLE.TYPE <- "Categorical" | ||
| DATE.VARIABLE.TYPE <- "Date" | ||
| DATE.TIME.VARIABLE.TYPE <- "Date/Time" | ||
| DURATION.VARIABLE.TYPE <- "Duration" | ||
|
|
||
| #' @param var.types A character vector containing variable types (see function | ||
| #' variableType). | ||
|
|
@@ -342,6 +360,9 @@ splitByComma <- function(input.text, ignore.commas.in.parentheses = FALSE) | |
| result <- result[result != ""] | ||
| result | ||
| } | ||
| result <- trimws(result) | ||
| result <- result[result != ""] | ||
| result | ||
| } | ||
|
|
||
| #' @param x A vector. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
condition.funclosure captures the exceptionefrom the outer scope, but this creates a dependency on the outer variable that may not be the intended behavior. Whencondition.funis called with different messages, it still checksinherits(e, "UserError")based on the original exception, which may not match the current context wherecondition.funis being invoked.