Improve error handling on signature mismatch cases

and cleanup old todos that have been addressed
This commit is contained in:
Kristen Newbury
2022-11-23 14:06:23 -05:00
parent 01b248a2a9
commit 2bda917a4e
3 changed files with 12 additions and 20 deletions

View File

@@ -43,7 +43,6 @@ def load(fname):
try:
content = json.load(fp)
except json.decoder.JSONDecodeError as err:
# TODO knewbury error handling
logging.error('Error reading from {}: {}: line {}, column {}'
.format(fname, err.msg, err.lineno, err.colno))
status_writer.file_load_error["file"] = fname
@@ -69,18 +68,14 @@ sarif_struct = signature.fillsig(args, sarif_struct, context)
#
# Use reference type graph (signature) to traverse sarif and attach values to tables
#
# try:
# tgraph = typegraph.Typegraph(signature_single.struct_graph_2022_02_01)
# typegraph.destructure(tgraph, signature_single.start_node_2022_02_01, sarif_struct)
# except json.decoder.JSONDecodeError as err:
# logging.error('Error reading from {}: {}: line {}, column {}'
# .format(args.file, err.msg, err.lineno, err.colno))
# sys.exit(1)
tgraph = typegraph.Typegraph(signature_single.struct_graph_2022_02_01)
typegraph.destructure(tgraph, signature_single.start_node_2022_02_01, sarif_struct)
# may have gathered warnings below, if not does nothing
status_writer.csv_write_warnings()
try:
tgraph = typegraph.Typegraph(signature_single.struct_graph_2022_02_01)
typegraph.destructure(tgraph, signature_single.start_node_2022_02_01, sarif_struct)
except Exception:
# will have gathered errors/warnings
status_writer.csv_write_warnings()
#pass the exception up to be put into log by runner
raise(Exception)
#
# Form output tables

View File

@@ -126,7 +126,7 @@ def joins_for_scans(basetables, external_info, scantables):
"id" : e.scan_id,
"commit_id" : b.project.revisionId[0],
"project_id" : e.project_id,
# TODO extract real date information from somewhere
# TODO extract real date information from somewhere external
"db_create_start" : pd.Timestamp(0.0, unit='s'),
"db_create_stop" : pd.Timestamp(0.0, unit='s'),
"scan_start_date" : pd.Timestamp(0.0, unit='s'),
@@ -165,7 +165,6 @@ def joins_for_results(basetables, external_info):
res = pd.concat(stack)
else:
if stack == []:
# TODO knewbury to error handling
logging.warning("Zero problem/path_problem results found in sarif "
"file but processing anyway.")
status_writer.csv_write(status_writer.zero_results)

View File

@@ -113,7 +113,8 @@ def destructure(typegraph: Typegraph, node: NodeId, tree: Tree):
elif t in [str, int, bool]:
pass
else:
# TODO knewbury error handling
status_writer.unknown_sarif_parsing_shape["extra_info"] = "Unhandled type: %s" % t
status_writer.csv_write(status_writer.unknown_sarif_parsing_shape)
raise Exception("Unhandled type: %s" % t)
def _destructure_dict_1(typegraph, node, tree):
@@ -139,7 +140,7 @@ def _destructure_dict_1(typegraph, node, tree):
# Sanity check
sig = typegraph.signature_graph[node]
if type(sig) != tuple:
# TODO knewbury error handling
# TODO add error handling?
raise SignatureMismatch()
# Destructure this dictionary
@@ -160,10 +161,8 @@ def _destructure_dict(typegraph: Typegraph, node, tree):
type_fields = typegraph.fields[node]
if tree_fields == type_fields:
_destructure_dict_1(typegraph, node, tree)
# TODO knewbury error handling here
elif set(tree_fields).issuperset(set(type_fields)):
# Log a warning
# log.warning("XX: Tree has unrecognized fields")
logging.warning('Input tree has unrecognized fields, collecting only '
'known entries: {}'.format(tree))
logging.warning('tree fields: {}'.format(sorted(tree_fields)))
@@ -189,7 +188,6 @@ def _destructure_dict(typegraph: Typegraph, node, tree):
)
else:
# TODO knewbury error handling
status_writer.unknown_sarif_parsing_shape["extra_info"] = "type fields {} do not match tree fields {}.".format(type_fields, tree_fields)
status_writer.csv_write(status_writer.unknown_sarif_parsing_shape)
raise Exception("typegraph: unhandled case reached: cannot match type "