Fedde
Fedde

Reputation: 19

UnboundLocalError: local variable 'transaction_id' referenced before assignment

I need some help in dentifying this error. Python is new to me so trying to learn everyday.

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()

When it triggers, it gives me the error. I have been searching and found something about setting transaction_id global. This confuse me.

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

Hopefully some guru out there can help me to lead the way!

Upvotes: 1

Views: 569

Answers (2)

chepner
chepner

Reputation: 531345

It would appear that

self.coindaemon.walletpassphrase(options.walletpassword, 60)

raises an error other than bitcoinrpc.exceptions.WalletAlreadyUnlocked, so the inner try statement fails to catch it and it is caught by the outer try. As a result, transaction_id is never set before it is used in the except Exception clause.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599630

If self.coindaemon.sendfrom raises an exception, transaction_id will not be set, so it can't be logged inside the except clause. You could try setting the variable to a default value before that line.

Upvotes: 1

Related Questions