dwins/gsconfig.py — Issue #20: More human exception text for XML Parsing Errors.
Author: ingenieroariel
Created: 2011-10-13 17:27:06+00:00
State: open

Sometimes the get_xml method fails with messages like:

```
unclosed token: line 42, column 2
```

The following patch raises a more human message, along with the offending URL and logs the content of the response for debugging:

```
diff --git a/src/geoserver/catalog.py b/src/geoserver/catalog.py
index 5cfe023..724148b 100644
--- a/src/geoserver/catalog.py
+++ b/src/geoserver/catalog.py
@@ -11,9 +11,9 @@ from os import unlink
import httplib2
from zipfile import is_zipfile
from xml.etree.ElementTree import XML
+from xml.parsers.expat import ExpatError
from urlparse import urlparse
from urllib import urlencode
-
logger = logging.getLogger("gsconfig.catalog")

class UploadError(Exception):
@@ -103,7 +103,19 @@ class Catalog(object):
response, content = self.http.request(url)
if response.status == 200:
self._cache[url] = (datetime.now(), content)
- return XML(content)
+ # Before attempting to parse it, verify that it does not contain
+ # more than one declaration. This behavior is common for GeoServer
+ # and hard to debug at the ElemTree level.
+ try:
+ parsed_content = XML(content)
+ except ExpatError, e:
+ msg = "Invalid XML found at url '%s'." % url
+ e.args = (msg,)
+ logger.exception(e)
+ logger.debug(content)
+ raise e
+ else:
+ return parsed_content
else:
raise FailedRequestError("Tried to make a GET request to %s but got a %d status code: \n%s" % (url, response.status, content))

```

Giving an output like:

```
ExpatError: Invalid XML found at url 'http://localhost/geoserver/rest/workspaces/geonode/datastores/GFDRRDetailed_Water_Bodies/featuretypes/MOZ_Detailed_Water_Bodies.xml'.
```

Patch in raw format available at:

https://gist.github.com/1284858