Search code examples
pythonsparqlhttp-errorsparqlwrapper

HTTP Error 500 in SPARQL Query


when I run a Python code of the form

from SPARQLWrapper import SPARQLWrapper, JSON 
from urllib import request, error
import time

class SomeClass: 

...

  def SomeFunction(self): 

    EndPoint =  SPARQLWrapper('http://collection.britishmuseum.org/sparql')
    Query = '''
            SomeString
            '''
          
    EndPoint.setQuery(Query)
    EndPoint.setReturnFormat(JSON)
  
    try: 
      Result = EndPoint.query().convert()
    except error.HTTPError: 
      time.sleep(SomeTime)
      SomeFunction()
      
SomeInstance = SomeClass()
SomeInstance.SomeFunction()

my exception catches HTTP Errors with a 502 code (Bad gateway) that sometimes occur at the endpoint. However, there is a specific HTTP Error that it doesn't catch, thus breaking the program at some point.

The error states the following:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self)
    656         try:
--> 657             response = urlopener(request)
    658             return response, self.returnFormat

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in open(self, fullurl, data, timeout)
    531             meth = getattr(processor, meth_name)
--> 532             response = meth(req, response)
    533 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_response(self, request, response)
    641             response = self.parent.error(
--> 642                 'http', request, response, code, msg, hdrs)
    643 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in error(self, proto, *args)
    563         args = (dict, proto, meth_name) + args
--> 564         result = self._call_chain(*args)
    565         if result:

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_error_302(self, req, fp, code, msg, headers)
    755 
--> 756         return self.parent.open(new, timeout=req.timeout)
    757 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in open(self, fullurl, data, timeout)
    531             meth = getattr(processor, meth_name)
--> 532             response = meth(req, response)
    533 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_response(self, request, response)
    641             response = self.parent.error(
--> 642                 'http', request, response, code, msg, hdrs)
    643 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in error(self, proto, *args)
    569             args = (dict, 'default', 'http_error_default') + orig_args
--> 570             return self._call_chain(*args)
    571 

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:

c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
    649     def http_error_default(self, req, fp, code, msg, hdrs):
--> 650         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    651 

HTTPError: HTTP Error 500: Connect to bigdata:8080 [bigdata/172.17.0.4] failed: Connection refused

During handling of the above exception, another exception occurred:

EndPointInternalError                     Traceback (most recent call last)
<ipython-input-6-46c4c3ac2387> in <module>()
      1 SomeInstance = SomeClass()
----> 2 SomeInstance.SomeFunction()

<ipython-input-4-d513cf0b30b5> in SomeFunction(self)
     90         EndPoint.setQuery(Query) 
     91         EndPoint.setReturnFormat(JSON)
     92         try:
---> 93             Result = EndPoint.query().convert()
     95         except error.HTTPError:

c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in query(self)
    685             @rtype: L{QueryResult} instance
    686         """
--> 687         return QueryResult(self._query())
    688 
    689     def queryAndConvert(self):

c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self)
    663                 raise EndPointNotFound(e.read())
    664             elif e.code == 500:
--> 665                 raise EndPointInternalError(e.read())
    666             else:
    667                 raise e

EndPointInternalError: EndPointInternalError: endpoint returned code 500 and response

How can I solve this issue -- i.e. catch this error with the same exception and keep the program running?

Thank you!


Solution

  • Looking at the stack trace, you also have to handle an EndPointInternalError from SPARQLExceptions (see the end of your error stacktrace), thus the following should work:

    try: 
      Result = EndPoint.query().convert()
    except error.HTTPError: 
      time.sleep(SomeTime)
      SomeFunction()
    except EndPointInternalError:
      # whatever code needed to handle this case
    

    And if you want to treat both errors the same way:

     try: 
          Result = EndPoint.query().convert()
     except (error.HTTPError, EndPointInternalError): 
          time.sleep(SomeTime)
          SomeFunction()