From f8bdef83e4885c41eab56ce5d7c968ca610efab9 Mon Sep 17 00:00:00 2001 From: Nicolas Raillard Date: Mon, 5 Jan 2026 09:26:57 +0100 Subject: [PATCH 1/2] fix errors on CRAN when remote resource is unavalable and more robust tests --- DESCRIPTION | 3 +-- R/spectral_data_download.R | 32 ++++++++++++++++++----- tests/testthat/test-data_download.R | 40 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ceb57df..f024075 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,8 +42,7 @@ Suggests: knitr, mockery, rmarkdown, - testthat, - vdiffr + testthat LinkingTo: Rcpp, RcppArmadillo diff --git a/R/spectral_data_download.R b/R/spectral_data_download.R index f5ac83b..e9a0565 100644 --- a/R/spectral_data_download.R +++ b/R/spectral_data_download.R @@ -8,7 +8,6 @@ #' @noRd #' @keywords internal download_nc_data <- function(url, destfile) { - # Ensure destfile exists only if successful success <- tryCatch( { @@ -16,13 +15,18 @@ download_nc_data <- function(url, destfile) { TRUE }, error = function(e) { - message("Could not download spectral data. - The remote server may be unavailable or the URL may have changed.") + message( + "Could not download spectral data. + The remote server may be unavailable or the URL may have changed." + ) FALSE }, warning = function(w) { - message("A warning occurred while downloading the spectral data. - The resource may have changed.\n", w) + message( + "A warning occurred while downloading the spectral data. + The resource may have changed.\n", + w + ) FALSE } ) @@ -62,11 +66,18 @@ get_2d_spectrum_raw <- function(point, year, month) { "_spec.nc" ) - temp <- tempfile(fileext = ".nc") file <- download_nc_data(url, temp) + if (is.null(file)) { + message( + "Could not download spectral data. + The remote server may be unavailable or the URL may have changed." + ) + return(NULL) + } + nc <- ncdf4::nc_open(file) on.exit({ @@ -136,6 +147,14 @@ get_1d_spectrum_raw <- function(point, year, month) { file <- download_nc_data(url, temp) + if (is.null(file)) { + message( + "Could not download spectral data. + The remote server may be unavailable or the URL may have changed." + ) + return(NULL) + } + nc <- ncdf4::nc_open(file) on.exit({ @@ -339,7 +358,6 @@ get_2d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") { get_1d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") { stopifnot(length(point) == 1) - if (is.numeric(point)) { point <- resourcecodedata::rscd_spectral[point, "name"] } diff --git a/tests/testthat/test-data_download.R b/tests/testthat/test-data_download.R index dab7861..d3c588f 100644 --- a/tests/testthat/test-data_download.R +++ b/tests/testthat/test-data_download.R @@ -182,3 +182,43 @@ test_that("download_nc_data() fails gracefully when FTP not available", { expect_null(result) }) + +test_that("get_1d_spectrum() fails gracefully when FTP not available", { + download_nc_data <- getFromNamespace("download_nc_data", "resourcecode") + # Mock curl_download to throw an error (simulating network failure) + mock_download <- function(...) stop("FTP connection failed") + + # Replace curl_download inside the function + testthat::local_mocked_bindings(download_nc_data = function(...) NULL) + + expect_message( + result <- get_1d_spectrum( + "SEMREVO", + start = "1994-01-01", + end = "1994-02-28" + ), + "Could not download spectral data" + ) + + expect_null(result) +}) + +test_that("get_2d_spectrum() fails gracefully when FTP not available", { + download_nc_data <- getFromNamespace("download_nc_data", "resourcecode") + # Mock curl_download to throw an error (simulating network failure) + mock_download <- function(...) stop("FTP connection failed") + + # Replace curl_download inside the function + testthat::local_mocked_bindings(download_nc_data = function(...) NULL) + + expect_message( + result <- get_2d_spectrum( + "SEMREVO", + start = "1994-01-01", + end = "1994-02-28" + ), + "Could not download spectral data" + ) + + expect_null(result) +}) From 9de0bfa104296604f05ae81b0478d150a71a5f57 Mon Sep 17 00:00:00 2001 From: Nicolas Raillard Date: Mon, 5 Jan 2026 09:39:23 +0100 Subject: [PATCH 2/2] Update DESCRIPTION and NEWS to prepare next release --- DESCRIPTION | 4 ++-- NEWS.md | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f024075..d06e0ea 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: resourcecode Title: Access to the 'RESOURCECODE' Hindcast Database -Version: 0.5.1.9000 -Date: 2025-12-16 +Version: 0.5.2 +Date: 2026-01-05 Authors@R: person("Nicolas", "Raillard", , "nicolas.raillard@ifremer.fr", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3385-5104")) diff --git a/NEWS.md b/NEWS.md index 5dcbc8b..935a159 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ -# resourcecode (development version) +# resourcecode 0.5.2 + +- Give informative message when remote database is not available # resourcecode 0.5.1