Skip to content

Commit 4b447a6

Browse files
authored
Merge pull request #11 from Resourcecode-project/CRAN_issue_2025
Fix CRAN issue
2 parents 64cab64 + 9de0bfa commit 4b447a6

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: resourcecode
22
Title: Access to the 'RESOURCECODE' Hindcast Database
3-
Version: 0.5.1.9000
4-
Date: 2025-12-16
3+
Version: 0.5.2
4+
Date: 2026-01-05
55
Authors@R:
66
person("Nicolas", "Raillard", , "nicolas.raillard@ifremer.fr", role = c("aut", "cre"),
77
comment = c(ORCID = "0000-0003-3385-5104"))
@@ -42,8 +42,7 @@ Suggests:
4242
knitr,
4343
mockery,
4444
rmarkdown,
45-
testthat,
46-
vdiffr
45+
testthat
4746
LinkingTo:
4847
Rcpp,
4948
RcppArmadillo

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# resourcecode (development version)
1+
# resourcecode 0.5.2
2+
3+
- Give informative message when remote database is not available
24

35
# resourcecode 0.5.1
46

R/spectral_data_download.R

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@
88
#' @noRd
99
#' @keywords internal
1010
download_nc_data <- function(url, destfile) {
11-
1211
# Ensure destfile exists only if successful
1312
success <- tryCatch(
1413
{
1514
curl::curl_download(url, destfile = destfile, mode = "wb")
1615
TRUE
1716
},
1817
error = function(e) {
19-
message("Could not download spectral data.
20-
The remote server may be unavailable or the URL may have changed.")
18+
message(
19+
"Could not download spectral data.
20+
The remote server may be unavailable or the URL may have changed."
21+
)
2122
FALSE
2223
},
2324
warning = function(w) {
24-
message("A warning occurred while downloading the spectral data.
25-
The resource may have changed.\n", w)
25+
message(
26+
"A warning occurred while downloading the spectral data.
27+
The resource may have changed.\n",
28+
w
29+
)
2630
FALSE
2731
}
2832
)
@@ -62,11 +66,18 @@ get_2d_spectrum_raw <- function(point, year, month) {
6266
"_spec.nc"
6367
)
6468

65-
6669
temp <- tempfile(fileext = ".nc")
6770

6871
file <- download_nc_data(url, temp)
6972

73+
if (is.null(file)) {
74+
message(
75+
"Could not download spectral data.
76+
The remote server may be unavailable or the URL may have changed."
77+
)
78+
return(NULL)
79+
}
80+
7081
nc <- ncdf4::nc_open(file)
7182

7283
on.exit({
@@ -136,6 +147,14 @@ get_1d_spectrum_raw <- function(point, year, month) {
136147

137148
file <- download_nc_data(url, temp)
138149

150+
if (is.null(file)) {
151+
message(
152+
"Could not download spectral data.
153+
The remote server may be unavailable or the URL may have changed."
154+
)
155+
return(NULL)
156+
}
157+
139158
nc <- ncdf4::nc_open(file)
140159

141160
on.exit({
@@ -339,7 +358,6 @@ get_2d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
339358
get_1d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
340359
stopifnot(length(point) == 1)
341360

342-
343361
if (is.numeric(point)) {
344362
point <- resourcecodedata::rscd_spectral[point, "name"]
345363
}

tests/testthat/test-data_download.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,43 @@ test_that("download_nc_data() fails gracefully when FTP not available", {
182182

183183
expect_null(result)
184184
})
185+
186+
test_that("get_1d_spectrum() fails gracefully when FTP not available", {
187+
download_nc_data <- getFromNamespace("download_nc_data", "resourcecode")
188+
# Mock curl_download to throw an error (simulating network failure)
189+
mock_download <- function(...) stop("FTP connection failed")
190+
191+
# Replace curl_download inside the function
192+
testthat::local_mocked_bindings(download_nc_data = function(...) NULL)
193+
194+
expect_message(
195+
result <- get_1d_spectrum(
196+
"SEMREVO",
197+
start = "1994-01-01",
198+
end = "1994-02-28"
199+
),
200+
"Could not download spectral data"
201+
)
202+
203+
expect_null(result)
204+
})
205+
206+
test_that("get_2d_spectrum() fails gracefully when FTP not available", {
207+
download_nc_data <- getFromNamespace("download_nc_data", "resourcecode")
208+
# Mock curl_download to throw an error (simulating network failure)
209+
mock_download <- function(...) stop("FTP connection failed")
210+
211+
# Replace curl_download inside the function
212+
testthat::local_mocked_bindings(download_nc_data = function(...) NULL)
213+
214+
expect_message(
215+
result <- get_2d_spectrum(
216+
"SEMREVO",
217+
start = "1994-01-01",
218+
end = "1994-02-28"
219+
),
220+
"Could not download spectral data"
221+
)
222+
223+
expect_null(result)
224+
})

0 commit comments

Comments
 (0)