我需要一些帮助来识别此错误。 Python对我来说是新手,所以每天都要学习。
class WithdrawHandler(BaseHandler):
def get(self):
self.redirect(u"/")
@gen.coroutine
def post(self):
if not self.current_user:
self.set_status(403, "Forbidden. Please log in first.")
return
withdraw_address = json.loads(self.get_argument("withdrawAddress", None))
username = tornado.escape.json_decode(self.current_user)
withdraw_amount_str = json.loads(self.get_argument("withdrawAmount", None))
withdraw_amount = None
try:
withdraw_amount = float(withdraw_amount_str)
except Exception:
logging.exception("Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
self.set_status(400, "Invalid amount to withdraw.")
self.finish()
return
if not self.coindaemon.validateaddress(withdraw_address).isvalid:
logging.info("[Invalid Withdraw Address] Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
self.set_status(400, "Invalid withdraw address. Please enter a valid value.")
self.finish()
return
if float(self.coindaemon.getbalance(username, minconf=2)) < withdraw_amount:
logging.info("[Insufficient Funds] Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
self.set_status(400, "Insufficient funds to withdraw.")
self.finish()
return
transaction_time = get_sql_datetime()
try:
try:
self.coindaemon.walletpassphrase(options.walletpassword, 60)
except bitcoinrpc.exceptions.WalletAlreadyUnlocked:
pass
transaction_id = self.coindaemon.sendfrom(username, withdraw_address, withdraw_amount, minconf=2, comment="Withdraw")
query = 'insert into transactions (transaction_id, transaction_type, transaction_time, username, amount, withdraw_address) values (%s, %s, %s, %s, %s, %s);'
addtransaction = yield momoko.Op(self.db.execute, query, (transaction_id, "withdraw", transaction_time, username, withdraw_amount, withdraw_address))
balance = self.coindaemon.getbalance(username, minconf=2)
self.write(dict(bal=str(balance)))
self.set_status(200)
except Exception:
logging.exception("Transaction ID: " + transaction_id + " | Username: " + username + " | Withdraw Address: " + withdraw_address + " | Withdraw Amount: " + withdraw_amount_str + " | Balance: " + str(balance))
self.set_status(400, "Error withdrawing.")
finally:
self.finish()
当它触发时,它会给我错误。我一直在搜索并发现有关设置transaction_id global的信息。这让我很困惑。
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1221, in _when_complete
if result.result() is not None:
File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 227, in wrapper
runner.run()
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 531, in run
yielded = self.gen.send(next)
File "main.py", line 282, in post
logging.exception("Transaction ID: " + transaction_id + " | Username: " + username + " | Withdraw Address: " + withdraw_address + " | Withdraw Amount: " + withdraw_amount_str + " | Balance: " + str(balance))
UnboundLocalError: local variable 'transaction_id' referenced before assignment
希望那里的一些大师可以帮助我带路!
答案 0 :(得分:1)
如果self.coindaemon.sendfrom
引发异常,则不会设置transaction_id
,因此无法在except子句中记录。您可以尝试在该行之前将变量设置为默认值。
答案 1 :(得分:1)
看来
self.coindaemon.walletpassphrase(options.walletpassword, 60)
引发bitcoinrpc.exceptions.WalletAlreadyUnlocked
以外的错误,因此内部try
语句无法捕获它,并且它被外部try
捕获。因此,在transaction_id
子句中使用except Exception
之前永远不会设置{{1}}。