exceptions.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.exceptions
  4. ~~~~~~~~~~~~~~~~~
  5. This module contains all exceptions used by the Celery API.
  6. """
  7. from __future__ import absolute_import
  8. from billiard.exceptions import ( # noqa
  9. SoftTimeLimitExceeded, TimeLimitExceeded, WorkerLostError, Terminated,
  10. )
  11. UNREGISTERED_FMT = """\
  12. Task of kind %s is not registered, please make sure it's imported.\
  13. """
  14. class SecurityError(Exception):
  15. """Security related exceptions.
  16. Handle with care.
  17. """
  18. class Ignore(Exception):
  19. """A task can raise this to ignore doing state updates."""
  20. class SystemTerminate(SystemExit):
  21. """Signals that the worker should terminate."""
  22. class QueueNotFound(KeyError):
  23. """Task routed to a queue not in CELERY_QUEUES."""
  24. class ImproperlyConfigured(ImportError):
  25. """Celery is somehow improperly configured."""
  26. class NotRegistered(KeyError):
  27. """The task is not registered."""
  28. def __repr__(self):
  29. return UNREGISTERED_FMT % str(self)
  30. class AlreadyRegistered(Exception):
  31. """The task is already registered."""
  32. class TimeoutError(Exception):
  33. """The operation timed out."""
  34. class MaxRetriesExceededError(Exception):
  35. """The tasks max restart limit has been exceeded."""
  36. class RetryTaskError(Exception):
  37. """The task is to be retried later."""
  38. #: Optional message describing context of retry.
  39. message = None
  40. #: Exception (if any) that caused the retry to happen.
  41. exc = None
  42. #: Time of retry (ETA), either int or :class:`~datetime.datetime`.
  43. when = None
  44. def __init__(self, message=None, exc=None, when=None, **kwargs):
  45. from kombu.utils.encoding import safe_repr
  46. self.message = message
  47. if isinstance(exc, basestring):
  48. self.exc, self.excs = None, exc
  49. else:
  50. self.exc, self.excs = exc, safe_repr(exc) if exc else None
  51. self.when = when
  52. Exception.__init__(self, exc, when, **kwargs)
  53. def humanize(self):
  54. if isinstance(self.when, int):
  55. return 'in %ss' % self.when
  56. return 'at %s' % (self.when, )
  57. def __str__(self):
  58. if self.message:
  59. return self.message
  60. if self.excs:
  61. return 'Retry %s: %r' % (self.humanize(), self.excs)
  62. return 'Retry %s' % self.humanize()
  63. def __reduce__(self):
  64. return self.__class__, (self.message, self.excs, self.when)
  65. class TaskRevokedError(Exception):
  66. """The task has been revoked, so no result available."""
  67. class NotConfigured(UserWarning):
  68. """Celery has not been configured, as no config module has been found."""
  69. class AlwaysEagerIgnored(UserWarning):
  70. """send_task ignores CELERY_ALWAYS_EAGER option"""
  71. class InvalidTaskError(Exception):
  72. """The task has invalid data or is not properly constructed."""
  73. class CPendingDeprecationWarning(PendingDeprecationWarning):
  74. pass
  75. class CDeprecationWarning(DeprecationWarning):
  76. pass
  77. class IncompleteStream(Exception):
  78. """Found the end of a stream of data, but the data is not yet complete."""
  79. class ChordError(Exception):
  80. """A task part of the chord raised an exception."""