我下面有python flask代码,该代码从回溯中创建JSON对象。
我要尝试的只是将此异常记录到Postgres表中,但是它没有处理该异常,而是多次抛出该异常。我不确定为什么尝试将其插入表时多次抛出相同的异常。 如果我只注释插入语句,那么一切都可以正常工作,URL甚至返回相同的回溯对象。
@app.errorhandler(Exception)
def exceptions(error):
timestamp = strftime('[%Y-%b-%d %H:%M]')
tb = str(traceback.format_exc())
print( timestamp, request.remote_addr, request.method, request.scheme, request.full_path,socket.gethostbyaddr(request.remote_addr)[0],tb)
error_json_object ={
"time":timestamp,
"remote_ip":request.remote_addr,
"remote_host":socket.gethostbyaddr(request.remote_addr)[0],
"request_path":request.full_path,
'request_method':request.method,
"error_stack":tb
}
dml_query = 'insert into error_logs values(\'{}\',\'{}\')'.format(timestamp,json.dumps({"name":tb}))
conn = psycopg2.connect(host="**",user="**",password="**",database="app_core",port=5432)
dml_cursor = conn.cursor()
dml_cursor.execute(dml_query)
conn.commit();
conn.close()
return json.dumps(error_json_object),500