From 847dd09e535e65e85ff199710570e8fe4b7d2765 Mon Sep 17 00:00:00 2001 From: Alex Axthelm Date: Tue, 23 Apr 2024 08:00:39 +0200 Subject: [PATCH] Feat/41 check for empty downloads (#44) --- R/get_currency_exchange_rates.R | 11 +++++++++-- R/get_index_regions.R | 8 ++++++++ R/get_ishares_index_data.R | 5 +++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/R/get_currency_exchange_rates.R b/R/get_currency_exchange_rates.R index d34b07c..c3fd3e3 100644 --- a/R/get_currency_exchange_rates.R +++ b/R/get_currency_exchange_rates.R @@ -32,7 +32,7 @@ get_currency_exchange_rates <- year ) - httr2::request(url) %>% + raw_data <- httr2::request(url) %>% httr2::req_headers("Accept" = "application/json") %>% httr2::req_timeout(max_seconds) %>% httr2::req_retry( @@ -47,7 +47,14 @@ get_currency_exchange_rates <- httr2::resp_body_json(simplifyVector = TRUE) %>% .[["CompactData"]] %>% .[["DataSet"]] %>% - .[["Series"]] %>% + .[["Series"]] + + if (is.null(raw_data)) { + log_error("No data found for the specified quarter: {quarter}.") + stop("No data found for the specified quarter.") + } + + raw_data %>% dplyr::rowwise() %>% dplyr::mutate(Obs = list(as.data.frame(.data$Obs))) %>% tidyr::unnest(cols = "Obs") %>% diff --git a/R/get_index_regions.R b/R/get_index_regions.R index d4fc267..8ba5c55 100644 --- a/R/get_index_regions.R +++ b/R/get_index_regions.R @@ -34,6 +34,10 @@ get_index_regions <- function() { ct$get("JSON.stringify(chartDataDMJson)"), flatten = TRUE ) + if (length(dm_data$categories) == 0L) { + log_error("No data found for Developed Markets.") + stop("No data found for Developed Markets.") + } log_debug("extracting data from script.") dm_countries <- c() @@ -72,6 +76,10 @@ get_index_regions <- function() { ct$get("JSON.stringify(chartDataEMJson)"), flatten = TRUE ) + if (length(data$categories) == 0L) { + log_error("No data found for Emerging Markets.") + stop("No data found for Emerging Markets.") + } log_debug("extracting data from script.") em_countries <- c() diff --git a/R/get_ishares_index_data.R b/R/get_ishares_index_data.R index c891cf0..82d2eae 100644 --- a/R/get_ishares_index_data.R +++ b/R/get_ishares_index_data.R @@ -44,6 +44,11 @@ get_ishares_index_data <- function(url, name, as_of_date) { # JSON string contains (illegal) UTF8 byte-order-mark! jsonlite::fromJSON(data_path)$aaData ) + if(length(raw_data) == 0L) { + log_error("No data found for the specified date.") + stop("No data found for the specified date.") + } + fixed_data <- fix_data(raw_data, data_names)