tsionyx
tsionyx

Reputation: 1669

Celery incorrectly propagates exceptions

I have this simple celery task:

@celery.task
def test_exception():
    raise ValueError("foo")

And I want to use the get method to get the result if my task happens to finish fast.

But, instead of expected builtin ValueError, I get the celery.backends.base.ValueError. How can I get the original ValueError in such a case?

>>> from tasks import test_exception
>>> try:
...     test_exception.delay().get()
... except ValueError as e:
...     print("Success: %s", str(e))
... except:
...     E=sys.exc_info()
...
>>> E
(<class 'celery.backends.base.ValueError'>, ValueError(u'foo',), <traceback object at 0x7f858c7f5ea8>)
>>> E[0].__module__
'celery.backends.base'

Upvotes: 2

Views: 1479

Answers (1)

Xealot
Xealot

Reputation: 1659

There is a bug in their tracker for this issue. It has to do with Celery serializing the Exception and not being able to reliably deserialize it.

https://github.com/celery/celery/issues/3586

https://github.com/celery/celery/issues/3758

It appears like they will not fix it. The somewhat unsightly and complete awful workaround I came up with was just to check for the name of the exception class.

if e.__class__.__name__ == "MyUniquelyNamedException":
    ...
else:
    raise

Upvotes: 2

Related Questions