如何从 ProgrammingError 异常中获取错误消息?

时间:2021-06-08 14:31:53

标签: python python-3.x exception amazon-redshift

我正在使用 redshift_connector,但我不知道如何只提取失败查询的错误消息而不将异常解析为字符串。我无法使用 dict() 在对象上找到任何属性,而且我不确定异常的来源是如何设置的,但我只想要消息。

如果我这样打印:

print(f"ERROR: {getattr(ex, 'message', repr(ex))}")

它没有找到属性“message”,所以默认为:

redshift_connector.error.ProgrammingError: {'S': 'ERROR', 'C': 'XX000', 'M': 'Received error 503 from server', 'D': '\n  -----------------------------------------------\n  error:  Received error 503 from server\n  code:      32004\n  context:   \n  query:     657118\n  location:  exfunc_data.cpp:312\n  process:   query0_109_657118 [pid=27456]\n  -----------------------------------------------\n', 'F': '/home/ec2-user/padb/src/sys/xen_execute.cpp', 'L': '9801', 'R': 'pg_throw'}

最初的想法是用它制作一个 dict(),但结果是空的。

如果我这样做,我可以得到“M”(消息):

json.loads(str(ex).replace("'", '"').replace("\n", "\\n"))['M']

...但这太恶心了。必须有更好的方法来解决这个问题吗?

有什么想法吗?

0 个答案:

没有答案
相关问题