Commits

gweis committed 10284a5

improve logging of errors in httpfourstore bindings

  • Participants
  • Parent commits 9b8c271

Comments (0)

Files changed (1)

ordf/handler/httpfourstore.py

 from urlparse import urljoin
 from urllib import quote, urlencode
 from urllib2 import Request
-from urllib2 import HTTPBasicAuthHandler, build_opener, HTTPPasswordMgrWithDefaultRealm
+from urllib2 import (HTTPBasicAuthHandler, build_opener,
+                     HTTPPasswordMgrWithDefaultRealm, URLError)
 from cStringIO import StringIO
 #from rdflib.util import from_n3
 #from collections import namedtuple
 
 LOG = logging.getLogger(__name__)
 
+
 class HTTPFourStore(HandlerPlugin):
 
     def __init__(self, *args, **kw):
         #headers['accept'] = 'application/x-turtle' .. not supported?
         # also support xml, and json?
         r = Request(self.sparqlurl, query, headers)
-        response = self.opener.open(r)
-        # TODO: catch errors here
-        x = Graph(identifier=ident)
-        x.parse(response, format='turtle')
-        LOG.debug("loaded %d triples from %s", len(x), x)
-        # TODO: catch errors here
+        x = None
+        try:
+            response = self.opener.open(r)
+            # TODO: catch errors here
+            x = Graph(identifier=ident)
+            x.parse(response, format='turtle')
+        except URLError as e:
+            LOG.fatal("Can't connect fetch graph %s from triplestore: %s'", ident, e)
+            raise
+            #x = Graph(identifier=ident)
         return x
-        
+
     def __setitem__(self, key, value):
         """
         Implement in concrete subclasses
         url = '%s?%s' % (self.dataurl, urlencode(params))
         r = Request(url, data.getvalue(), headers)
         r.get_method = lambda: 'PUT'
-        response = self.opener.open(r)
+        try:
+            response = self.opener.open(r)
+        except URLError as e:
+            LOG.fatal("Can't write to triplestore: %s", e)
+            LOG.debug("Attempted request: %s", data.getvalue())
+            raise
         # TODO: catch errors here
-        
+
     def __delitem__(self, key):
         """
         Implement in concrete subclasses
         url = '%s?%s' % (self.dataurl, urlencode(params))
         r = Request(url)
         r.get_method = lambda: 'DELETE'
-        response = self.opener.open(r)
+        try:
+            response = self.opener.open(r)
+        except URLError as e:
+            LOG.fatal("Can't delete graph %s to triplestore: %s", ident, e)
+            raise
         # TODO: catch errors here
 
     def append(self, frag):
         headers = {'content-type': 'application/x-www-form-urlencoded'}
         r = Request(self.dataurl, urlencode(params), headers)
         #r.get_method = lambda: 'POST'
-        response = self.opener.open(r)
+        try:
+            response = self.opener.open(r)
+        except URLError as e:
+            LOG.fatal("Can't append to triplestore: %s", e)
+            LOG.debug("Attempted request: %s", data.getvalue())
+            raise
         # TODO: catch errors here
 
     def query(self, query):
         params = {'query': query,
                   #'soft-limit': <set to something not overwhelming>,
                   'output': 'text',  # future versions will support this
-                  }        
+                  }
         # sparqlurl /sparql/ form-encodod post
         #    values: query, soft-limit, output(text, sparql, json), deault-graph-uri, apikey, callback
         #    flags: restricted,
             headers['accept'] = 'text/turtle'
         r = Request(self.sparqlurl, urlencode(params), headers)
         LOG.debug('start query')
-        response = self.opener.open(r)
+        try:
+            response = self.opener.open(r)
+        except Exception as e:
+            LOG.fatal("query failed: %s", e)
+            LOG.debug("Attempted query: %s", query)
+            return []
         LOG.debug('finished query')
-        
+
         # updateurl /update/ form-encoded post
         #    update, apikey
 
             result.parse(response, format='turtle')
         LOG.debug('return parsed result of len %d', len(result))
         return result
-        
+
         #construct or describe: return Graph...
         #otherwise list?
-        
+
     def commit(self):
         """
         Impement in concrete subclasses