Skip to content

Commit

Permalink
Added a better error message when the PRISM web service is down.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabutler-usbr committed Nov 28, 2023
1 parent 1086834 commit 2b13265
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Removed global assignment inside `prism_webservice()`
* Documented return value for `pd_stack()`, `prism_set_dl_dir()` ,`pd_to_file()`, `pd_get_type()` & `get_prism_*()` functions.
* Added examples to documentation for `pd_to_file()`, `prism_archive_clean()`, `prism_archive_verify()`, `prism_set_dl_dir()`, & `pd_get_*()` functions.
* Added a better error message when the PRISM webservice is down (not returning status 200). (#122)

# prism 0.2.1

Expand Down
33 changes: 33 additions & 0 deletions R/prism_webservice.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,
{
## Get file name
x <- httr::HEAD(uri)
x2 <- check_status(x, uri)

# check status - return NULL if status was not 200
if (!isTRUE(x2)) {
warning(x2)
return(NULL)
}

fn <- x$headers[["content-disposition"]]
fn <- regmatches(fn, regexpr('\\"[a-zA-Z0-9_\\.]+', fn))
fn <- substr(fn, 2, nchar((fn)))
Expand Down Expand Up @@ -98,6 +106,31 @@ prism_webservice <- function(uri, keepZip = FALSE, returnName = FALSE,
}
}

check_status <- function(x, uri) {
if (x$status_code == as.integer(200)) {
# status 200 is request has succeeded
return(TRUE)
} else {
# otherwise - tell user about status
y <- paste0(
"\nAttempted to download:\n",
uri,
'\nCould not reach url, with status:\n',
x$status_code, "\n",
'Please try pasting one or more of the following urls into your web browser:\n',
'https://services.nacse.org/prism/data/public/4km/tmin/20090405', '\n',
'https://services.nacse.org/prism/data/public/4km/tmin/200904', '\n',
'https://services.nacse.org/prism/data/public/4km/tmin/2009', '\n',
'https://services.nacse.org/prism/data/public/4km/tmin/1944', '\n',
'If you receive the same status code, then the issue is with the PRISM web service.\n',
'Please try again later or use the manual downloads at https://prism.oregonstate.edu.\n',
'If the status is different or download works, please file a bug at:\n',
'https://github.com/ropensci/prism/issues/'
)
return(y)
}
}

# x is the file to check.
# returns TRUE if it is a zip file, and otherwiser returns text that was read
check_zip_file <- function(x) {
Expand Down

0 comments on commit 2b13265

Please sign in to comment.