Commits

william  committed d047bd6

Commit bugfix to handle exceptions thrown when the url cannot be opened

  • Participants
  • Parent commits bdbe632

Comments (0)

Files changed (1)

File src/oembed_works/oembed.py

             OEmbedResponse object according to data fetched 
             
         '''        
-        opener = self._urllib.build_opener()
-        opener.addheaders = self._requestHeaders.items()
+        try:
+            opener = self._urllib.build_opener()
+            opener.addheaders = self._requestHeaders.items()
 
-        response = opener.open(url)
+            response = opener.open(url)
 
-        headers = response.info()
-        raw = response.read()
+            headers = response.info()
+            raw = response.read()
 
-        if not headers.has_key('Content-Type'):
-            raise OEmbedError('Missing mime-type in response')
+            if not headers.has_key('Content-Type'):
+                raise OEmbedError('Missing mime-type in response')
         
-        if headers['Content-Type'].find('application/xml') != -1 or \
-           headers['Content-Type'].find('text/xml') != -1:
-            response = OEmbedResponse.newFromXML(raw)
+            if headers['Content-Type'].find('application/xml') != -1 or \
+                headers['Content-Type'].find('text/xml') != -1:
+                response = OEmbedResponse.newFromXML(raw)
             
-        elif headers['Content-Type'].find('application/json') != -1 or \
-             headers['Content-Type'].find('text/json') != -1:
-            response = OEmbedResponse.newFromJSON(raw)
+            elif headers['Content-Type'].find('application/json') != -1 or \
+                headers['Content-Type'].find('text/json') != -1:
+                response = OEmbedResponse.newFromJSON(raw)
         
-        else:
-            raise OEmbedError('Invalid mime-type in response - %s' % headers['Content-Type'])
+            else:
+                raise OEmbedError('Invalid mime-type in response - %s' % headers['Content-Type'])
+        except HTTPError e:
+            raise OEmbedError('Error opening oembed url %s' % str(e))
         
         return response