Skip to content

Commit 811b3c1

Browse files
committed
Fixed error handling.
1 parent d4e9597 commit 811b3c1

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

celery_haystack/tasks.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,34 @@ def get_model_class(self, object_path, **kwargs):
5757
model_class = get_model(app_name, classname)
5858

5959
if model_class is None:
60-
logger = self.get_logger(**kwargs)
61-
logger.error("Could not load model "
62-
"from '%s'. Moving on..." % object_path)
63-
return None
64-
60+
raise ImproperlyConfigured("Could not load model '%s'." %
61+
object_path)
6562
return model_class
6663

6764
def get_instance(self, model_class, pk, **kwargs):
6865
"""
6966
Fetch the instance in a standarized way.
7067
"""
7168
logger = self.get_logger(**kwargs)
69+
instance = None
7270
try:
7371
instance = model_class.objects.get(pk=pk)
7472
except model_class.DoesNotExist:
7573
logger.error("Couldn't load model instance "
76-
"with pk #%s. Somehow it went missing?" % pk)
77-
return None
74+
"with pk %s. Somehow it went missing?" % pk)
7875
except model_class.MultipleObjectsReturned:
79-
logger.error("More than one object with pk #%s. Oops?" % pk)
80-
return None
81-
76+
logger.error("More than one object with pk %s. Oops?" % pk)
8277
return instance
8378

8479
def get_index(self, model_class, **kwargs):
8580
"""
8681
Fetch the model's registered ``SearchIndex`` in a standarized way.
8782
"""
88-
logger = self.get_logger(**kwargs)
8983
try:
9084
return index_holder.get_index(model_class)
9185
except IndexNotFoundException:
92-
logger.error("Couldn't find a SearchIndex for %s." % model_class)
86+
raise ImproperlyConfigured("Couldn't find a SearchIndex for %s." %
87+
model_class)
9388
return None
9489

9590
def get_handler_options(self, **kwargs):
@@ -108,8 +103,9 @@ def run(self, action, identifier, **kwargs):
108103
# First get the object path and pk (e.g. ('notes.note', 23))
109104
object_path, pk = self.split_identifier(identifier, **kwargs)
110105
if object_path is None or pk is None:
111-
logger.error("Skipping.")
112-
return
106+
msg = "Couldn't handle object with identifier %s" % identifier
107+
logger.error(msg)
108+
raise ValueError(msg)
113109

114110
# Then get the model class for the object path
115111
model_class = self.get_model_class(object_path, **kwargs)
@@ -123,32 +119,37 @@ def run(self, action, identifier, **kwargs):
123119
current_index.remove_object(identifier, **handler_options)
124120
except Exception, exc:
125121
logger.error(exc)
126-
self.retry([action, identifier], kwargs, exc=exc)
122+
self.retry(exc=exc)
127123
else:
128-
logger.debug("Deleted '%s' from index" % identifier)
129-
return
124+
msg = ("Deleted '%s' from index %s" %
125+
(identifier, current_index))
126+
logger.debug(msg)
127+
return msg
130128

131129
elif action == 'update':
132130
# and the instance of the model class with the pk
133131
instance = self.get_instance(model_class, pk, **kwargs)
134132
if instance is None:
135-
logger.debug("Didn't update index for '%s'" % identifier)
136-
return
133+
logger.debug("Didn't update index %s for '%s'" %
134+
(current_index, identifier))
135+
raise ValueError("Couldn't load object '%s'" % identifier)
137136

138137
# Call the appropriate handler of the current index and
139138
# handle exception if neccessary
140-
logger.debug("Indexing '%s'." % instance)
141139
try:
142140
handler_options = self.get_handler_options(**kwargs)
143141
current_index.update_object(instance, **handler_options)
144142
except Exception, exc:
145143
logger.error(exc)
146-
self.retry([action, identifier], kwargs, exc=exc)
144+
self.retry(exc=exc)
147145
else:
148-
logger.debug("Updated index with '%s'" % instance)
146+
msg = ("Updated index %s with '%s'" %
147+
(current_index, instance))
148+
logger.debug(msg)
149+
return msg
149150
else:
150151
logger.error("Unrecognized action '%s'. Moving on..." % action)
151-
self.retry([action, identifier], kwargs, exc=exc)
152+
raise ValueError("Unrecognized action %s" % action)
152153

153154

154155
class CeleryHaystackUpdateIndex(Task):

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ v0.6 (2012-06-27)
1515

1616
CELERY_HAYSTACK_TRANSACTION_SAFE = False
1717

18+
* Refactored the error handling to always return a message about what
19+
happened in every step of the index interaction. Raise exception about
20+
misconfiguration and wrong parameters quicker.
21+
1822
.. _`django-celery-transactions`: https://github.com/chrisdoble/django-celery-transactions
1923
.. _`Celery's user guide`: http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions
2024

0 commit comments

Comments
 (0)