Skip to content
Prev 17531 / 21307 Next

[Bioc-devel] How to handle Empty reply from server from httr package

On the one hand, if the error is from curl I think this is the case

url <- "localhost:8000"  ## nothing to connect to
tryCatch({
    response <- GET(url)
    stop_for_status(response)
    content(response)
}, error = function(e) {
    message(paste(class(e), collapse = " "))
    stop("curl example: ", conditionMessage(e))
})
        
simpleError error condition
Error in value[[3L]](cond) : 
  curl example: Failed to connect to localhost port 8000: Connection refused

whereas if the server responds with an error then 

url <- "http://httpbin.org/status/500"
tryCatch({
    response <- GET(url)
    stop_for_status(response)
    content(response)
}, error=function(e){
    message(paste(class(e), collapse = " "))
    stop("stop_for_status example: ", conditionMessage(e))
})

http_500 http_error error condition
Error in value[[3L]](cond) : 
  stop_for_status example: Internal Server Error (HTTP 500).

Noting the different error classes, it looks like

tryCatch({
    response <- GET(url)
    stop_for_status(response)
    content(response)
}, http_error = function(e) {
    stop("stop_for_status error: ", conditionMessage(e))
}, error = function(e) {
    stop("other error: ", conditionMessage(e))
})

would allow selective handling of each.

It looks like your pseudocode would have handled a curl error, but not an error response from the server, but that's not consistent with what you stated your problem was so maybe I've misunderstood.

Martin

?On 12/10/20, 12:16 PM, "Bioc-devel on behalf of Jianhong Ou, Ph.D." <bioc-devel-bounces at r-project.org on behalf of jianhong.ou at duke.edu> wrote:

    Hi all,

    Sometime my package got "Empty reply from server" error when I using httr package.
    The pseudo-code I used to handle error is like this:

    library(httr)
    APIurl <- "https://www.ebi.ac.uk/proteins/api/features?offset=0&size=-1&reviewed=true&types=DNA_BIND%2CMOTIF%2CDOMAIN&taxid=9606&accession=K7PPA8%2CP04637%2CQ53GA5%2CH2EHT1%2CA0A087X1Q1%2CA0A087WXZ1%2CA0A087WT22"
    tryCatch({
    response <- GET(featureURL)
    if(!http_error(response)){    content <- httr::content(response)}
    }, error=function(e){
      message(e)
    },warning=function(w){
      message(w)
    },interrupt=function(i){
      message(i)
    })

    But I did not get the error handled. What is the correct way to handle the curl 52 error?
    Thank you for your help.


    Yours Sincerely,

    Jianhong Ou

    Email: jianhong.ou at duke.edu
    Bioinformatician II
    Department of Cell Biology
    Duke University School of Medicine
    Durham, NC, 27710

    Confidentiality Notice:\ This e-mail message, including ...{{dropped:12}}

    _______________________________________________
    Bioc-devel at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/bioc-devel
Message-ID: <BL0PR04MB66091163009A36D71440DA34F9CB0@BL0PR04MB6609.namprd04.prod.outlook.com>
In-Reply-To: <DM6PR05MB5931A85ED574154ABBD70AE489CB0@DM6PR05MB5931.namprd05.prod.outlook.com>