Skip to content

Instantly share code, notes, and snippets.

@sckott
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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?

@spacedman
Copy link

All you get from http is a status code - in this case 500, which means "Internal Server Error". The server can return anything it likes in the body, there's no standard for passing more error info except as text after the status code. I'm not sure if compliant servers can vary what they put after the status code...

You can get the status code from a$headers$status and the message in a$headers$statusmessage but thats it.

@spacedman
Copy link

The "reason phrases" are suggestions: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Status_codes so if the server wants to say something more explanatory it could!

@sckott
Copy link
Author

sckott commented Feb 10, 2014

Hi @spacedman ! Thanks for the comments and the explanation. That makes sense. Right, I'm familiar with a$header$statusmessage, but was curious if that more detailed message could be collected. Thanks!

@hadley
Copy link

hadley commented Feb 11, 2014

As @spacedman points out, this is something that varies from API to API. I think you need to implement something by hand for each API to hook up to.

@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