Skip to content

Commit

Permalink
query_gn(): look for error in title of output
Browse files Browse the repository at this point in the history
  • Loading branch information
kbroman committed Oct 25, 2022
1 parent 173e7d3 commit e7d3190
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions R/query_gn.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,36 @@ query_gn <-

result <- httr::GET(url)
httr::stop_for_status(result)

if(error_in_title(result, encoding)) {
stop(attr(error_in_title(result, encoding), "error_text"))
}

result <- httr::content(result, encoding=encoding, as=output)

result[vapply(result, is.null, TRUE)] <- NA # added to avoid having as.data.frame(result) crash

check_gn_error(result)
}

# if the result is a web page with Error: in the title
error_in_title <-
function(result, encoding="UTF-8")
{
result_text <- httr::content(result, encoding=encoding, as="text")
error <- regexpr("<title>Error:.*</title>", result_text)
if(error < 0) return(FALSE)
else {
error_text <- substr(result_text, error+13, error+attr(error, "match.length")-23)
error_text <- gsub("&#34;", '"', error_text)
error_text <- gsub("&#39;", "'", error_text)
result <- TRUE
attr(result, "error_text") <- error_text
return(result)
}
}


# check whether the result is an error
check_gn_error <-
function(result)
Expand Down

0 comments on commit e7d3190

Please sign in to comment.