[hooks,sobjects] Do not fail in hooks because of CKAN synchronization errors
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 22 Jan 2015 18:14:14 +0100
changeset 70 5ab8c18d8056
parent 68 dd318b930f1d
child 72 e0f76f26f987
[hooks,sobjects] Do not fail in hooks because of CKAN synchronization errors Just log an error instead (except in "test" mode as this will be otherwise hardly debuggable). Add a specific exception that could be catched when using services directly. Closes #4869939.
hooks.py
sobjects.py
--- a/hooks.py	Thu Jan 22 16:51:35 2015 +0100
+++ b/hooks.py	Thu Jan 22 18:14:14 2015 +0100
@@ -21,7 +21,8 @@
 from cubicweb.server import hook
 
 from cubes.ckanpublish.sobjects import (delete_dataset,
-                                        delete_dataset_resource)
+                                        delete_dataset_resource,
+                                        CKANSyncError)
 from cubes.ckanpublish.utils import ckan_instance_configured
 
 
@@ -56,12 +57,20 @@
 
     def precommit_event(self):
         for eid in self.get_data():
-            if self.cnx.deleted_in_transaction(eid):
-                datasetid = self.cnx.entity_from_eid(eid).ckan_dataset_id
-                delete_dataset(self.cnx.vreg.config, eid, datasetid)
-                self.info('deleted CKAN dataset %s', datasetid)
-            else:
-                self.cnx.call_service('ckanpublish.sync_dataset', eid=eid)
+            event = ('deletion' if self.cnx.deleted_in_transaction(eid)
+                     else 'synchronization')
+            try:
+                if event == 'deletion':
+                    datasetid = self.cnx.entity_from_eid(eid).ckan_dataset_id
+                    delete_dataset(self.cnx.vreg.config, eid, datasetid)
+                    self.info('deleted CKAN dataset %s', datasetid)
+                else:
+                    self.cnx.call_service('ckanpublish.sync_dataset', eid=eid)
+            except CKANSyncError as exc:
+                self.error('%s of CKAN dataset linked to entity #%d failed: %s',
+                           event, eid, str(exc))
+                if self.cnx.vreg.config.mode == 'test':
+                    raise
 
 
 class DeleteCKANResourceHook(hook.Hook):
@@ -132,8 +141,17 @@
     def precommit_event(self):
         for eid in self.get_data():
             resourceid = self.cnx.entity_from_eid(eid).ckan_resource_id
-            if self.cnx.deleted_in_transaction(eid) and resourceid is not None:
-                delete_dataset_resource(self.cnx.vreg.config, eid, resourceid)
-                self.info('deleted resource %s', resourceid)
-            else:
-                self.cnx.call_service('ckanpublish.sync_resource', eid=eid)
+            event = ('deletion' if (self.cnx.deleted_in_transaction(eid)
+                                    and resourceid is not None)
+                     else 'synchronization')
+            try:
+                if event == 'deletion':
+                    delete_dataset_resource(self.cnx.vreg.config, eid, resourceid)
+                    self.info('deleted resource %s', resourceid)
+                else:
+                    self.cnx.call_service('ckanpublish.sync_resource', eid=eid)
+            except CKANSyncError as exc:
+                self.error('%s of CKAN resource linked to entity #%d failed: %s',
+                           event, eid, str(exc))
+                if self.cnx.vreg.config.mode == 'test':
+                    raise
--- a/sobjects.py	Thu Jan 22 16:51:35 2015 +0100
+++ b/sobjects.py	Thu Jan 22 18:14:14 2015 +0100
@@ -18,20 +18,22 @@
 
 from requests.exceptions import RequestException
 
-from cubicweb import ValidationError
 from cubicweb.server import Service
 from cubicweb.predicates import adaptable
 
 from cubes.ckanpublish.utils import (ckan_instance_configured, ckan_post,
                                      CKANPostError)
 
+class CKANSyncError(Exception):
+    """Error during synchronization to CKAN instance"""
+
 
 def _ckan_action(config, eid, action, **kwargs):
-    """Run `ckan_post` and eventually raise ValidationError."""
+    """Run `ckan_post` and eventually raise CKANSyncError."""
     try:
         return ckan_post(config, action, **kwargs)
     except (CKANPostError, RequestException) as exc:
-        raise ValidationError(eid, {'ckan_dataset_id': unicode(exc)})
+        raise CKANSyncError(str(exc))
 
 
 def create_dataset(config, eid, data):