adding error messages to registrar' response to blockstack info

This commit is contained in:
Aaron Blankstein
2017-06-06 15:06:04 -04:00
parent a59da6fab4
commit 87ba3b86d9
3 changed files with 78 additions and 1 deletions

View File

@@ -39,6 +39,13 @@ CREATE TABLE entries( fqu STRING NOT NULL,
PRIMARY KEY(fqu,queue_id) );
"""
ERROR_SQL = """
CREATE TABLE entry_errs( fqu STRING NOT NULL,
queue_id STRING NOT NULL,
errormsg STRING NOT NULL,
PRIMARY KEY(fqu,queue_id,errormsg));
"""
from ..config import MAX_TX_CONFIRMATIONS
from ..logger import get_logger
@@ -50,12 +57,13 @@ def queuedb_create( path ):
Create all the tables and indexes we need.
"""
global QUEUE_SQL
global QUEUE_SQL, ERROR_SQL
if os.path.exists( path ):
raise Exception("Database '%s' already exists" % path)
lines = [l + ";" for l in QUEUE_SQL.split(";")]
lines += [l + ";" for l in ERROR_SQL.split(";")]
con = sqlite3.connect( path, isolation_level=None )
for line in lines:
@@ -64,6 +72,17 @@ def queuedb_create( path ):
con.row_factory = queuedb_row_factory
return con
def conditionally_create_err_table( sql_conn ):
"""
Creates a table for storing registrar errors,
if that table doesn't already exist.
"""
global ERROR_SQL
creation_sql = ERROR_SQL.replace("CREATE TABLE",
"CREATE TABLE IF NOT EXISTS")
lines = [l + ";" for l in creation_sql.split(";")]
assert len(lines) == 2
sql_conn.execute(lines[0])
def queuedb_open( path ):
"""
@@ -73,6 +92,7 @@ def queuedb_open( path ):
con = queuedb_create( path )
else:
con = sqlite3.connect( path, isolation_level=None )
conditionally_create_err_table( con )
con.row_factory = queuedb_row_factory
return con
@@ -159,6 +179,55 @@ def queuedb_find( queue_id, fqu, limit=None, path=DEFAULT_QUEUE_PATH ):
db.close()
return ret
def queue_add_error_msg( queue_id, fqu, error_msg, path=DEFAULT_QUEUE_PATH ):
"""
Add an error message for an entry
"""
sql = """INSERT or REPLACE INTO entry_errs (fqu, queue_id, errormsg)
VALUES (?, ?, ?);"""
args = (fqu, queue_id, error_msg)
db = queuedb_open(path)
if db is None:
raise Exception("Failed to open %s" % path)
cur = db.cursor()
rows = queuedb_query_execute( cur, sql, args )
db.commit()
db.close()
def queue_clear_error_msg( queue_id, fqu, path=DEFAULT_QUEUE_PATH ):
"""
Remove all error messages for an entry
"""
sql = "DELETE FROM entry_errs WHERE fqu = ? AND queue_id = ?;"
args = (fqu, queue_id)
db = queuedb_open(path)
if db is None:
raise Exception("Failed to open %s" % path)
cur = db.cursor()
rows = queuedb_query_execute( cur, sql, args )
db.commit()
db.close()
def queue_get_error_msgs( queue_id, fqu, path=DEFAULT_QUEUE_PATH ):
"""
Return any error messages associate with an entry
"""
sql = "SELECT errormsg FROM entry_errs WHERE fqu = ? AND queue_id = ?;"
args = (fqu, queue_id)
db = queuedb_open(path)
if db is None:
raise Exception("Failed to open %s" % path)
cur = db.cursor()
rows = queuedb_query_execute( cur, sql, args )
ret = [ r['errormsg'] for r in rows ]
db.commit()
db.close()
return ret
def queuedb_findall( queue_id, limit=None, path=DEFAULT_QUEUE_PATH ):
"""
@@ -575,6 +644,9 @@ def get_queue_state(queue_ids=None, limit=None, path=DEFAULT_QUEUE_PATH):
rows = [extract_entry(r) for r in raw_rows]
state += rows
for row in state:
row['errors'] = queue_get_error_msgs(row['type'], row['fqu'], path=path)
return state
@@ -612,6 +684,8 @@ def queue_removeall( entries, path=DEFAULT_QUEUE_PATH ):
rc = queuedb_remove( entry['type'], entry['fqu'], entry['tx_hash'], path=path )
if not rc:
raise Exception("Failed to remove %s.%s.%s" % (entry['type'], entry['fqu'], entry['tx_hash']))
# also clear out information about errors
queue_clear_error_msg( entry['type'], entry['fqu'], path=path )
return True