exceptions.py 3.3 KB

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