exceptions.py 2.8 KB

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