1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
import contextlib |
4
|
1 |
|
import logging |
5
|
|
|
|
6
|
1 |
|
from plumbum.commands.processes import ProcessExecutionError, CommandNotFound, ProcessTimedOut |
|
|
|
|
7
|
1 |
|
from celery.exceptions import (SecurityError, Ignore, QueueNotFound, WorkerShutdown, WorkerTerminate, |
|
|
|
|
8
|
|
|
ImproperlyConfigured, NotRegistered, AlreadyRegistered, |
9
|
|
|
MaxRetriesExceededError, Retry, |
10
|
|
|
TaskRevokedError, NotConfigured, AlwaysEagerIgnored, |
11
|
|
|
InvalidTaskError, ChordError, CPendingDeprecationWarning, |
12
|
|
|
CDeprecationWarning, FixupWarning, DuplicateNodenameWarning, |
13
|
|
|
SoftTimeLimitExceeded, TimeLimitExceeded, WorkerLostError, |
14
|
|
|
Terminated) |
15
|
1 |
|
from saruman.helpers.exceptions import FirewallGenericError |
16
|
|
|
|
17
|
1 |
|
__all__ = ['error_handling'] |
18
|
|
|
|
19
|
1 |
|
WARNING = (Ignore, AlreadyRegistered, CPendingDeprecationWarning, |
20
|
|
|
CDeprecationWarning, FixupWarning, DuplicateNodenameWarning, |
21
|
|
|
SoftTimeLimitExceeded, Retry) |
22
|
1 |
|
HANDLED_ERROR = () |
23
|
1 |
|
UNHANDLED_ERROR = (ProcessTimedOut, WorkerShutdown, WorkerTerminate, ImproperlyConfigured, NotRegistered, |
24
|
|
|
MaxRetriesExceededError, TaskRevokedError, NotConfigured, AlwaysEagerIgnored, InvalidTaskError, |
25
|
|
|
ChordError, FirewallGenericError, TimeLimitExceeded, WorkerLostError, Terminated) |
26
|
1 |
|
CRITICAL = (ProcessExecutionError, CommandNotFound, SecurityError, QueueNotFound) |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
@contextlib.contextmanager |
30
|
|
|
def error_handling(logger): |
|
|
|
|
31
|
1 |
|
try: |
32
|
1 |
|
assert isinstance(logger, logging.Logger) |
33
|
1 |
|
try: |
34
|
1 |
|
yield |
35
|
1 |
|
except WARNING as e: |
|
|
|
|
36
|
|
|
logger.warning(e) |
37
|
1 |
|
except UNHANDLED_ERROR as e: |
|
|
|
|
38
|
|
|
logger.error(e) |
39
|
|
|
raise |
40
|
1 |
|
except HANDLED_ERROR as e: |
|
|
|
|
41
|
|
|
logger.error(e) |
42
|
1 |
|
except CRITICAL as e: |
|
|
|
|
43
|
1 |
|
logger.critical(e) |
44
|
|
|
except Exception as e: |
|
|
|
|
45
|
|
|
print(e) |
46
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.