Move creation of errors to the model as thats a more natural home. Provide backwards compatibility.

This commit is contained in:
David Read 2015-12-02 08:15:13 +00:00
parent 414c33ac6b
commit c7021933a0
2 changed files with 35 additions and 28 deletions

View File

@ -3,7 +3,6 @@ import re
import uuid import uuid
from sqlalchemy.sql import update,and_, bindparam from sqlalchemy.sql import update,and_, bindparam
from sqlalchemy.exc import InvalidRequestError
from pylons import config from pylons import config
from ckan import plugins as p from ckan import plugins as p
@ -136,31 +135,8 @@ class HarvesterBase(SingletonPlugin):
return ideal_name[:PACKAGE_NAME_MAX_LENGTH-APPEND_MAX_CHARS] + \ return ideal_name[:PACKAGE_NAME_MAX_LENGTH-APPEND_MAX_CHARS] + \
str(uuid.uuid4())[:APPEND_MAX_CHARS] str(uuid.uuid4())[:APPEND_MAX_CHARS]
_save_gather_error = HarvestGatherError.create
def _save_gather_error(self, message, job): _save_object_error = HarvestObjectError.create
err = HarvestGatherError(message=message, job=job)
try:
err.save()
except InvalidRequestError:
Session.rollback()
err.save()
finally:
log.error(message)
def _save_object_error(self, message, obj, stage=u'Fetch', line=None):
err = HarvestObjectError(message=message,
object=obj,
stage=stage,
line=line)
try:
err.save()
except InvalidRequestError, e:
Session.rollback()
err.save()
finally:
log_message = '{0}, line {1}'.format(message,line) if line else message
log.debug(log_message)
def _get_user_name(self): def _get_user_name(self):
''' '''

View File

@ -10,6 +10,7 @@ from sqlalchemy import ForeignKey
from sqlalchemy import types from sqlalchemy import types
from sqlalchemy.engine.reflection import Inspector from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.orm import backref, relation from sqlalchemy.orm import backref, relation
from sqlalchemy.exc import InvalidRequestError
from ckan import model from ckan import model
from ckan import logic from ckan import logic
@ -153,13 +154,43 @@ class HarvestGatherError(HarvestDomainObject):
'''Gather errors are raised during the **gather** stage of a harvesting '''Gather errors are raised during the **gather** stage of a harvesting
job. job.
''' '''
pass @classmethod
def create(cls, message, job):
'''
Helper function to create an error object and save it.
'''
err = cls(message=message, job=job)
try:
err.save()
except InvalidRequestError:
Session.rollback()
err.save()
finally:
# No need to alert administrator so don't log as an error
log.info(message)
class HarvestObjectError(HarvestDomainObject): class HarvestObjectError(HarvestDomainObject):
'''Object errors are raised during the **fetch** or **import** stage of a '''Object errors are raised during the **fetch** or **import** stage of a
harvesting job, and are referenced to a specific harvest object. harvesting job, and are referenced to a specific harvest object.
''' '''
pass @classmethod
def create(cls, message, object, stage=u'Fetch', line=None):
'''
Helper function to create an error object and save it.
'''
err = cls(message=message, object=object,
stage=stage, line=line)
try:
err.save()
except InvalidRequestError:
Session.rollback()
err.save()
finally:
log_message = '{0}, line {1}'.format(message, line) \
if line else message
log.debug(log_message)
def harvest_object_before_insert_listener(mapper,connection,target): def harvest_object_before_insert_listener(mapper,connection,target):
''' '''