我正在尝试从错误对象读取属性,同时捕获异常。错误来自芹菜工人。
下面我显示了带有 Celery 错误的控制台:
[2019-12-15 16:38:41,442: ERROR/MainProcess] Task api.tasks.sendSms[139dbbbd-df85-4528-a131-973848fb4f80] raised unexpected: AttributeError("'ERR_UNKNOWN' object has no attribute 'Text'")
Traceback (most recent call last):
File "/share/backend/backendsmsgateapi/api/tasks.py", line 16, in sendSms
status = smsServiceObject.sendParcicularSms(receiver, template)
File "/share/backend/backendsmsgateapi/api/smsService.py", line 38, in sendParcicularSms
state_machine.SendSMS(message)
gammu.ERR_UNKNOWN: {'Text': 'Nieznany błąd.', 'Where': 'SendSMS', 'Code': 27}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/share/backend/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/share/backend/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 648, in __protected_call__
return self.run(*args, **kwargs)
File "/share/backend/backendsmsgateapi/api/tasks.py", line 20, in sendSms
message = getattr(error_object, "Text")
AttributeError: 'ERR_UNKNOWN' object has no attribute 'Text'
我的代码部分是:
@shared_task
def sendSms(template, receiver, duration):
try:
smsServiceObject = smsService()
status = smsServiceObject.sendParcicularSms(receiver, template)
time.sleep(duration)
except:
error_object = sys.exc_info()[1]
message = getattr(error_object, "Text")
data = {
"success" : False,
"error_info": message,
"receiver": receiver
}
return data
data = {
"success": True,
"receiver": receiver
}
return data
如您所见,问题出在:
message = getattr(error_obj, "Text")
但是为什么?在控制台对象中,ERROR_UKNOWN具有属性"文本",值为"Neznany błąd"。 为什么它不起作用?我将不胜感激的帮助。
尝试以下操作:
error_object = sys.exc_info()[1]
message = error_object.args[0]['Text']
data = {
"success" : False,
"error_info": message,
"receiver": receiver
}
return data