Skip to content

Commit 2a6d2c9

Browse files
committed
Align error returns with existing
1 parent a6deba5 commit 2a6d2c9

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

R/install.R

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ install_cmdstan <- function(dir = NULL,
155155
return(invisible(NULL))
156156
}
157157
tar_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet)
158-
if (!tar_downloaded) {
158+
if (inherits(tar_downloaded, "try-error")) {
159+
error_msg <- paste("Download of CmdStan failed with error:",
160+
attr(tar_downloaded, "condition")$message)
159161
if (!is.null(version)) {
160-
stop("Download of CmdStan failed. Please check if the supplied version number is valid.", call. = FALSE)
162+
error_msg <- paste0(error_msg, "\nPlease check if the supplied version number is valid.")
163+
} else if (!is.null(release_url)) {
164+
error_msg <- paste0(error_msg, "\nPlease check if the supplied release URL is valid.")
161165
}
162-
if (!is.null(release_url)) {
163-
stop("Download of CmdStan failed. Please check if the supplied release URL is valid.", call. = FALSE)
164-
}
165-
stop("Download of CmdStan failed. Please try again.", call. = FALSE)
166+
stop(error_msg, call. = FALSE)
166167
}
167168
message("* Download complete")
168169
message("* Unpacking archive...")
@@ -364,13 +365,17 @@ latest_released_version <- function(quiet=TRUE) {
364365
dest_file <- tempfile(pattern = "releases-", fileext = ".json")
365366
download_url <- "https://api.github.com/repos/stan-dev/cmdstan/releases/latest"
366367
release_list_downloaded <- download_with_retries(download_url, dest_file, quiet = quiet)
368+
if (inherits(release_list_downloaded, "try-error")) {
369+
stop("GitHub download of release list failed with error: ",
370+
attr(release_list_downloaded, "condition")$message,
371+
call. = FALSE)
372+
}
367373
release <- jsonlite::read_json(dest_file)
368374
sub("v", "", release$tag_name)
369375
}
370376

371377
try_download <- function(download_url, destination_file,
372-
quiet = TRUE,
373-
stop_on_error = TRUE) {
378+
quiet = TRUE) {
374379
download_status <- try(
375380
suppressWarnings(
376381
utils::download.file(url = download_url,
@@ -380,11 +385,6 @@ try_download <- function(download_url, destination_file,
380385
),
381386
silent = TRUE
382387
)
383-
if (inherits(download_status, "try-error") && isTRUE(stop_on_error)) {
384-
stop("Download failed with error message: ",
385-
attr(download_status, "condition")$message,
386-
call. = FALSE)
387-
}
388388
download_status
389389
}
390390

@@ -395,16 +395,14 @@ download_with_retries <- function(download_url,
395395
pause_sec = 5,
396396
quiet = TRUE) {
397397
download_rc <- try_download(download_url, destination_file,
398-
quiet = quiet, stop_on_error = FALSE)
398+
quiet = quiet)
399399
num_retries <- 0
400400
while (num_retries < retries && inherits(download_rc, "try-error")) {
401401
Sys.sleep(pause_sec)
402402
num_retries <- num_retries + 1
403-
download_rc <- try_download(download_url, destination_file,
404-
quiet = quiet,
405-
stop_on_error = (num_retries == (retries)))
403+
download_rc <- try_download(download_url, destination_file, quiet = quiet)
406404
}
407-
TRUE
405+
download_rc
408406
}
409407

410408
build_cmdstan <- function(dir,

tests/testthat/test-install.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ test_that("install_cmdstan() errors if it times out", {
7575
test_that("install_cmdstan() errors if invalid version or URL", {
7676
expect_error(
7777
install_cmdstan(version = "2.23.2", wsl = os_is_wsl()),
78-
"Download of CmdStan failed. Please check if the supplied version number is valid."
78+
"Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied version number is valid."
7979
)
8080
expect_error(
8181
install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz",
8282
wsl = os_is_wsl()),
83-
"Download of CmdStan failed. Please check if the supplied release URL is valid."
83+
"Download of CmdStan failed with error: cannot open URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.23.2/cmdstan-2.23.2.tar.gz'\nPlease check if the supplied release URL is valid."
8484
)
8585
expect_error(
8686
install_cmdstan(release_url = "https://github.com/stan-dev/cmdstan/releases/tag/v2.24.0", wsl = os_is_wsl()),
@@ -241,6 +241,6 @@ test_that("Download failures return error message", {
241241
c("http_proxy"="invalid","https_proxy"="invalid"),
242242
install_cmdstan(dir = dir, overwrite = TRUE)
243243
)},
244-
"Download failed with error message: cannot open URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'")
244+
"GitHub download of release list failed with error: cannot open URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'")
245245
})
246246

0 commit comments

Comments
 (0)