exceptions.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 {0} 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.format(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. def __init__(self, message=None, exc=None, when=None, **kwargs):
  39. from kombu.utils.encoding import safe_repr
  40. self.message = message
  41. if isinstance(exc, basestring):
  42. self.exc, self.excs = None, exc
  43. else:
  44. self.exc, self.excs = exc, safe_repr(exc) if exc else None
  45. self.when = when
  46. Exception.__init__(self, exc, when, **kwargs)
  47. def humanize(self):
  48. if isinstance(self.when, int):
  49. return 'in {0.when}s'.format(self)
  50. return 'at {0.when}'.format(self)
  51. def __str__(self):
  52. if self.message:
  53. return self.message
  54. if self.excs:
  55. return 'Retry {0}: {1!r}'.format(self.humanize(), self.excs)
  56. return 'Retry {0}'.format(self.humanize())
  57. def __reduce__(self):
  58. return self.__class__, (self.message, self.excs, self.when)
  59. class TaskRevokedError(Exception):
  60. """The task has been revoked, so no result available."""
  61. class NotConfigured(UserWarning):
  62. """Celery has not been configured, as no config module has been found."""
  63. class AlwaysEagerIgnored(UserWarning):
  64. """send_task ignores CELERY_ALWAYS_EAGER option"""
  65. class InvalidTaskError(Exception):
  66. """The task has invalid data or is not properly constructed."""
  67. class CPendingDeprecationWarning(PendingDeprecationWarning):
  68. pass
  69. class CDeprecationWarning(DeprecationWarning):
  70. pass
  71. class IncompleteStream(Exception):
  72. """Found the end of a stream of data, but the data is not yet complete."""