Commit 10508b91 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[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.
parent dd318b930f1d
......@@ -21,7 +21,8 @@ from cubicweb.predicates import adaptable, score_entity, PartialPredicateMixIn
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 @@ class CKANDatasetOp(hook.DataOperationMixIn, hook.Operation):
def precommit_event(self):
for eid in self.get_data():
if self.cnx.deleted_in_transaction(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 @@ class CKANResourceOp(hook.DataOperationMixIn, hook.Operation):
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:
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
......@@ -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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment