Skip to content

Commit 4dcbfd8

Browse files
committed
improve API query error handling
1 parent ec2acae commit 4dcbfd8

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ importFrom(httr,content)
5858
importFrom(httr,http_error)
5959
importFrom(httr,modify_url)
6060
importFrom(httr,status_code)
61+
importFrom(httr,stop_for_status)
6162
importFrom(jsonlite,fromJSON)
6263
importFrom(jsonlite,toJSON)
6364
importFrom(lubridate,"%within%")

R/GetDOIs.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ GetDOIs <- function(bib){
3737
json.res <- httr::POST("http://search.crossref.org/links", body = json.bib,
3838
config = list(add_headers = headers),
3939
encode = "json")
40-
if (http_error(json.res) ||
40+
status <- status_code(json.res)
41+
if (status != 200 ||
4142
!(json.res <- content(json.res, type = "application/json",
4243
encoding = "UTF-8"))[[2L]])
43-
message("Failed to retrieve any DOIs.")
44+
message(gettextf("Failed to retrieve any DOIs [%s].", status))
4445
else{
4546
matches <- vapply(json.res[[1L]], "[[", FALSE, "match")
4647
if (!any(matches))

R/ReadCrossRef.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ ReadCrossRef <- function(query = "", filter = list(), limit = 5, offset = 0,
212212
}
213213

214214
#' @keywords internal
215+
#' @importFrom httr GET status_code content http_error
215216
#' @noRd
216217
GetCrossRefBibTeX <- function(doi, tmp.file){
217218
## temp <- try(getURLContent(url=doi,
@@ -233,9 +234,9 @@ GetCrossRefBibTeX <- function(doi, tmp.file){
233234
if (http_error(temp) ||
234235
!grepl("^[[:space:]]*@", parsed, useBytes = TRUE)){
235236
doi.text <- sub("^https?://(dx[.])?doi.org/", "", doi)
236-
message(gettextf("Server error for doi %s, you may want to %s",
237-
dQuote(doi.text),
238-
"try again, or BibTeX unavailable for this doi"))
237+
message(gettextf("Server error [%s] for doi %s, you may want to%s",
238+
status_code(temp), dQuote(doi.text),
239+
" try again, or BibTeX unavailable for this doi"))
239240
return(1L)
240241
}
241242
}

R/ReadPubMed.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#' are available.
4343
#' @references \url{https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch}
4444
#' @family pubmed
45-
#' @importFrom httr GET http_error
45+
#' @importFrom httr GET http_error stop_for_status
4646
#' @importFrom xml2 read_xml xml_find_all xml_text
4747
#' @examples
4848
#' if (interactive() && !httr::http_error("https://eutils.ncbi.nlm.nih.gov/"))
@@ -67,8 +67,7 @@ ReadPubMed <- function(query, database = "PubMed", ...){
6767
.parms$usehistory <- "y"
6868
## results <- try(getForm(base.url, .params = .parms))
6969
results <- GET(base.url, query = .parms)
70-
if (http_error(results))
71-
stop("Unable to GET results")
70+
stop_for_status(results)
7271

7372
tdoc <- read_xml(results, encoding = "UTF-8")
7473

0 commit comments

Comments
 (0)