@@ -141,7 +141,7 @@ install_cmdstan <- function(dir = NULL,
141141 dir_cmdstan <- file.path(dir , cmdstan_ver )
142142 dest_file <- file.path(dir , tar_gz_file )
143143 } else {
144- ver <- latest_released_version()
144+ ver <- latest_released_version(quiet = quiet )
145145 message(" * Latest CmdStan release is v" , ver )
146146 cmdstan_ver <- paste0(" cmdstan-" , ver , cmdstan_arch_suffix(ver ))
147147 tar_gz_file <- paste0(cmdstan_ver , " .tar.gz" )
@@ -154,15 +154,16 @@ install_cmdstan <- function(dir = NULL,
154154 if (! check_install_dir(dir_cmdstan , overwrite )) {
155155 return (invisible (NULL ))
156156 }
157- tar_downloaded <- download_with_retries(download_url , dest_file )
158- if (! tar_downloaded ) {
157+ tar_downloaded <- download_with_retries(download_url , dest_file , quiet = quiet )
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 , " \n Please check if the supplied version number is valid." )
163+ } else if (! is.null(release_url )) {
164+ error_msg <- paste0(error_msg , " \n Please 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..." )
@@ -360,45 +361,48 @@ github_download_url <- function(version_number) {
360361}
361362
362363# get version number of latest release
363- latest_released_version <- function () {
364+ 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"
366- release_list_downloaded <- download_with_retries(download_url , dest_file )
367- if (! release_list_downloaded ) {
368- stop(" GitHub download of release list failed." , call. = FALSE )
367+ 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 )
369372 }
370373 release <- jsonlite :: read_json(dest_file )
371374 sub(" v" , " " , release $ tag_name )
372375}
373376
377+ try_download <- function (download_url , destination_file ,
378+ quiet = TRUE ) {
379+ download_status <- try(
380+ suppressWarnings(
381+ utils :: download.file(url = download_url ,
382+ destfile = destination_file ,
383+ quiet = quiet ,
384+ headers = github_auth_token())
385+ ),
386+ silent = TRUE
387+ )
388+ download_status
389+ }
390+
374391# download with retries and pauses
375392download_with_retries <- function (download_url ,
376393 destination_file ,
377394 retries = 5 ,
378395 pause_sec = 5 ,
379396 quiet = TRUE ) {
380-
381- download_rc <- 1
382- while (retries > 0 && download_rc != 0 ) {
383- try(
384- suppressWarnings(
385- download_rc <- utils :: download.file(url = download_url ,
386- destfile = destination_file ,
387- quiet = quiet ,
388- headers = github_auth_token())
389- ),
390- silent = TRUE
391- )
392- if (download_rc != 0 ) {
393- Sys.sleep(pause_sec )
394- }
395- retries <- retries - 1
396- }
397- if (download_rc == 0 ) {
398- TRUE
399- } else {
400- FALSE
397+ download_rc <- try_download(download_url , destination_file ,
398+ quiet = quiet )
399+ num_retries <- 0
400+ while (num_retries < retries && inherits(download_rc , " try-error" )) {
401+ Sys.sleep(pause_sec )
402+ num_retries <- num_retries + 1
403+ download_rc <- try_download(download_url , destination_file , quiet = quiet )
401404 }
405+ download_rc
402406}
403407
404408build_cmdstan <- function (dir ,
0 commit comments