From 2bda917a4e2abe296e9ed4be0eff6b53b20e813a Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 23 Nov 2022 14:06:23 -0500 Subject: [PATCH] Improve error handling on signature mismatch cases and cleanup old todos that have been addressed --- bin/sarif-extract-scans | 21 ++++++++------------- sarif_cli/scan_tables.py | 3 +-- sarif_cli/typegraph.py | 8 +++----- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/bin/sarif-extract-scans b/bin/sarif-extract-scans index f479f7d..94052e9 100755 --- a/bin/sarif-extract-scans +++ b/bin/sarif-extract-scans @@ -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 diff --git a/sarif_cli/scan_tables.py b/sarif_cli/scan_tables.py index be71b2c..d6b79a7 100644 --- a/sarif_cli/scan_tables.py +++ b/sarif_cli/scan_tables.py @@ -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) diff --git a/sarif_cli/typegraph.py b/sarif_cli/typegraph.py index 9b0dfc5..5761943 100644 --- a/sarif_cli/typegraph.py +++ b/sarif_cli/typegraph.py @@ -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 "