Make sure to always fail gracefully if server not accessible
See CRAN policy:
Packages which use Internet resources should fail gracefully with an informative message
if the resource is not available or has changed (and not give a check warning nor error).
at https://cran.r-project.org/web/packages/policies.html
Change-Id: I0c346c75786b8f5392694337254d6f19f91d7caf
diff --git a/R/KorAPQuery.R b/R/KorAPQuery.R
index 9bab575..eb2afbd 100644
--- a/R/KorAPQuery.R
+++ b/R/KorAPQuery.R
@@ -188,15 +188,22 @@
log.info(verbose, "Searching \"", query, "\" in \"", vc, "\"", sep =
"")
res = apiCall(kco, paste0(requestUrl, '&count=0'))
- log.info(verbose, ": ", res$meta$totalResults, " hits")
- if(!is.null(res$meta$cached))
- log.info(verbose, " [cached]\n")
- else
- log.info(verbose, ", took ", res$meta$benchmark, "\n", sep = "")
+ if (is.null(res)) {
+ log.info(verbose, " [failed]\n")
+ message("API call failed.")
+ totalResults <- 0
+ } else {
+ totalResults <-res$meta$totalResults
+ log.info(verbose, ": ", totalResults, " hits")
+ if(!is.null(res$meta$cached))
+ log.info(verbose, " [cached]\n")
+ else
+ log.info(verbose, ", took ", res$meta$benchmark, "\n", sep = "")
+ }
if (as.df)
data.frame(
query = query,
- totalResults = res$meta$totalResults,
+ totalResults = totalResults,
vc = vc,
webUIRequestUrl = webUIRequestUrl,
stringsAsFactors = FALSE
@@ -208,11 +215,11 @@
fields = fields,
requestUrl = requestUrl,
request = request,
- totalResults = res$meta$totalResults,
+ totalResults = totalResults,
vc = vc,
apiResponse = res,
webUIRequestUrl = webUIRequestUrl,
- hasMoreMatches = (res$meta$totalResults > 0),
+ hasMoreMatches = (totalResults > 0),
)
}
})