Skip to content

Instantly share code, notes, and snippets.

@sckott
Last active August 29, 2015 13:56
Show Gist options
  • Save sckott/8911176 to your computer and use it in GitHub Desktop.
Save sckott/8911176 to your computer and use it in GitHub Desktop.
Is there a general solution for parsing server error messages that aren't returned in the header in httr so that we can print error message for users in a package?

This is a call to the GBIF API. There is an intential error in the call that gives an error message from the server

com.spatial4j.core.exception.InvalidShapeException: Invalid latitude: latitudes are range -90 to 90: provided lat: [-125.0]

In R with httr:

library(httr)
call <- "http://api.gbif.org/v0.9/occurrence/search?geometry=POLYGON((38.4 -125, 40.9 -125, 40.9 -121.8, 38.4 -121.8, 38.4 -125))"
a <- GET(call)

None of the metadata from the call, as far as I know, gives the error message.

You can parse the message out

library(XML)
doc = htmlParse(content(a, as="text"))
xpathSApply(doc, "//h1", xmlValue)
[1] "HTTP Status 500 - com.spatial4j.core.exception.InvalidShapeException: Invalid latitude: latitudes are range -90 to 90: provided lat: [-125.0]"

But I wonder if there is a general solution available in httr that works beyond just this example.

Perhaps the server error above should be in the header?

@sckott
Copy link
Author

sckott commented Feb 11, 2014

Okay, thanks Hadley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment