-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.py
More file actions
81 lines (58 loc) · 2.13 KB
/
errors.py
File metadata and controls
81 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import socket
import traceback as tb
from datetime import datetime
from raccoon import Publisher
import settings as st
# from connectors.rabbitmq import RabbitMQPublisher
# String format to log when an unhandled Exception is raised
# {m} will be replaced by the Exception's message
# {t} will be replaced by the Exception type
# {tb} will be replaced by the execution traceback
ERROR_FORMAT = 'Unhandled {t}: {m}\nTraceback:\n{tb}'
def log_unhandled_exception(exc_type, exc_value, exc_tb):
process_exception(exc_value, exc_tb=exc_tb)
def process_exception(
exception,
excepcion_type=None,
msg=None,
exc_tb=None,
body=None):
try:
if not excepcion_type:
exc_type = type(exception).__name__
else:
exc_type = excepcion_type
if not msg:
message = str(exception)
else:
message = msg
if exc_tb is None:
exc_tb = tb.format_exc()
if not isinstance(exc_tb, str):
exc_tb = ''.join(tb.format_tb(exc_tb))
log_exception(exc_type, message, exc_tb, body)
msg = {
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"app_name": st.APP_NAME,
"server": socket.gethostname(),
"message": message,
"type": exc_type,
}
if body:
msg.update({
"tag": 'Message received involved in the error: ' + str(body),
})
with Publisher(st.RABBITMQ_SERVER, st.RABBITMQ_USER, st.RABBITMQ_PASSWORD, 'nerrors', exchange_type='direct') as bus:
bus.publish_msg(msg)
except BaseException:
st.logger.error('Error while processing another error')
def log_exception(exc_type, exc_value, exc_tb, body=None):
try:
msg = '\nBody: {}\n'.format(body) if body is not None else ''
msg += ERROR_FORMAT
msg = msg.replace('{t}', exc_type)
msg = msg.replace('{m}', str(exc_value))
msg = msg.replace('{tb}', exc_tb)
st.logger.error(msg)
except BaseException:
st.logger.error('Error while loggin another error')