Skip to content

Commit 1e09e47

Browse files
authored
Merge pull request #658 from cmu-delphi/ndefries/forward-gh-request-errors
Make sure GitHub date-request errors are raised
2 parents 83a436c + 157f022 commit 1e09e47

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

R-packages/evalcast/NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ importFrom(covidcast,covidcast_signal)
5151
importFrom(data.table,"%chin%")
5252
importFrom(data.table,fread)
5353
importFrom(dplyr,bind_rows)
54+
importFrom(httr,GET)
55+
importFrom(httr,RETRY)
5456
importFrom(magrittr,"%>%")
5557
importFrom(purrr,pmap)
5658
importFrom(rlang,":=")

R-packages/evalcast/NEWS.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
returns the most recent value within a given time period for either a `day`
55
or `epiweek` incidence period. For incidence signals, `get_target_response`
66
still sums all values within the time period.
7+
- Raise non-successful HTTP statuses as errors in
8+
`get_covidhub_forecast_dates`. This is especially useful for debugging
9+
issues with GitHub API authentication.
10+
- Stop `get_forecast_dates` from swallowing all errors raised by
11+
`get_covidhub_forecast_dates` and potentially silently returning bogus
12+
results, which can cause mysterious and hard-to-debug errors downstream.
13+
This means that `get_forecast_dates` may fail more often, but there are
14+
several benefits. First, without valid forecast dates, downstream calls
15+
won't get valid forecast data. Fetching forecast dates is fast, so the cost
16+
of rerunning is low, while downloading forecasts is time-consuming. This
17+
change also lets us verify GitHub API authentication upfront, which is
18+
necessary for forecast downloads later.
19+
- Retry HTTP requests in `get_forecast_dates` if they don't succeed
20+
initially.
721

822
# evalcast 0.3.4
923

R-packages/evalcast/R/get_covidhub_predictions.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,12 @@ get_forecast_dates <- function(forecasters,
148148
start_date,
149149
end_date,
150150
date_filtering_function) {
151-
forecast_dates <- as_date(forecast_dates)
152151
forecaster_dates <- vector("list", length = length(forecasters))
153152
for (i in seq_len(length(forecasters))) {
154-
forecaster_dates[[i]] <- tryCatch({
155-
lubridate::as_date(get_covidhub_forecast_dates(forecasters[i]))
156-
},
157-
error = function(e) cat(sprintf("%i. %s\n", i, e$message))
158-
)
153+
forecaster_dates[[i]] <- lubridate::as_date(get_covidhub_forecast_dates(forecasters[i]))
159154
}
160155
if (length(forecast_dates) != 0) {
156+
forecast_dates <- as_date(forecast_dates)
161157
# Intersect acts oddly with dates. If foo = as_date(bar), then foo == bar is
162158
# true, but (foo %in% bar) is false and intersect(foo, bar) is an empty
163159
# vector. Additionally, intersect returns a numeric object instead of a
@@ -431,6 +427,8 @@ get_forecaster_predictions_alt <- function(covidhub_forecaster_name,
431427
#'
432428
#' @return vector of forecast dates
433429
#'
430+
#' @importFrom httr GET RETRY
431+
#'
434432
#' @export
435433
get_covidhub_forecast_dates <- function(forecaster_name) {
436434
url <- "https://api.github.com/repos/reichlab/covid19-forecast-hub/git/trees/master"
@@ -451,25 +449,28 @@ get_covidhub_forecast_dates <- function(forecaster_name) {
451449

452450
# Get the URL for the submissions folder "data-processed".
453451
submissions_folder <- url %>%
454-
httr::GET(auth_header) %>%
452+
RETRY("GET", url = ., auth_header) %>%
455453
is_rate_limit_exceeded() %>%
454+
httr::stop_for_status() %>%
456455
httr::content() %>%
457456
purrr::pluck("tree") %>%
458457
magrittr::extract2(which(purrr::map_chr(., "path") == "data-processed"))
459458

460459
# Get the URL for the specified forecaster folder.
461460
forecaster_folder <- submissions_folder$url %>%
462-
httr::GET(auth_header) %>%
461+
RETRY("GET", url = ., auth_header) %>%
463462
is_rate_limit_exceeded() %>%
463+
httr::stop_for_status() %>%
464464
httr::content() %>%
465465
purrr::pluck("tree") %>%
466466
magrittr::extract2(which(purrr::map_chr(., "path") == forecaster_name))
467467

468468
# Get the forecaster submission files.
469469
submission_file_pattern <- sprintf("^(20\\d{2}-\\d{2}-\\d{2})-%s.csv$", forecaster_name)
470470
submission_files <- forecaster_folder$url %>%
471-
httr::GET(auth_header) %>%
471+
RETRY("GET", url = ., auth_header) %>%
472472
is_rate_limit_exceeded() %>%
473+
httr::stop_for_status() %>%
473474
httr::content() %>%
474475
purrr::pluck("tree") %>%
475476
purrr::map_chr("path") %>%

0 commit comments

Comments
 (0)