Skip to content

Commit c440797

Browse files
committed
better error message and allow user to control script's encoding
1 parent 4529dc3 commit c440797

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

R/automation.R

+18-3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ setMethod("scriptSavepoint", "Script", function(x) {
102102
#' @param is_file The default guesses whether a file or string was
103103
#' used in the `script` argument, but you can override the heuristics
104104
#' by specifying `TRUE` for a file, and `FALSE` for a string.
105+
#' @param encoding The encoding the script file is saved as on your
106+
#' machine. Defaults to UTF-8.
105107
#' @param ... Additional options, such as `dry_run = TRUE` passed on
106108
#' to the API
107109
#'
@@ -131,14 +133,20 @@ setMethod("scriptSavepoint", "Script", function(x) {
131133
#' }
132134
#' @export
133135
#' @seealso [`automation-undo`] & [`script-catalog`]
134-
runCrunchAutomation <- function(dataset, script, is_file = string_is_file_like(script), ...) {
136+
runCrunchAutomation <- function(
137+
dataset,
138+
script,
139+
is_file = string_is_file_like(script),
140+
encoding = "UTF-8",
141+
...
142+
) {
135143
reset_automation_error_env()
136144
stopifnot(is.dataset(dataset))
137145
stopifnot(is.character(script))
138146

139147
if (is_file) {
140148
automation_error_env$file <- script
141-
script <- readLines(script, encoding = "UTF-8", warn = FALSE)
149+
script <- readLines(script, encoding = encoding, warn = FALSE)
142150
} else {
143151
automation_error_env$file <- NULL
144152
}
@@ -207,7 +215,7 @@ crunchAutomationErrorHandler <- function(response) {
207215
msg <- http_status(response)$message
208216
automation_messages <- try(content(response)$resolution, silent = TRUE)
209217

210-
if (!is.error(automation_messages)) {
218+
if (!is.error(automation_messages) && !is.null(automation_messages)) {
211219
# dig into the response to get the script as we sent it to the server
212220
request_body <- fromJSON(rawToChar(response$request$options$postfields))
213221
automation_error_env$script <- request_body$body$body
@@ -244,6 +252,13 @@ crunchAutomationErrorHandler <- function(response) {
244252
}
245253

246254
msg <- paste("Crunch Automation Error\n", error_items, more_info_text, sep = "")
255+
} else {
256+
# could also have information in message property
257+
# try to use it if it's a character string
258+
other_msg <- try(content(response)[["message"]], silent = TRUE) #nocov
259+
if (is.character(other_msg)) { #nocov
260+
msg <- paste0(msg, " - ", paste0(other_msg, collapse = "\n")) #nocov
261+
}
247262
}
248263
halt(msg)
249264
}

man/runCrunchAutomation.Rd

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)