Imporve error message routing.
@@ -97,7 +97,6 @@ def run(input_files: KGTKFiles,
value_options.show(out=error_file)
print("=======", file=error_file, flush=True)
- try:
kgtk_file: Path
for kgtk_file in kgtk_files:
if verbose:
@@ -107,7 +106,9 @@ def run(input_files: KGTKFiles,
else:
print ("Validating from stdin", file=error_file, flush=True)
- kr: KgtkReader = KgtkReader.open(kgtk_file,
+ kr: typing.Optional[KgtkReader] = None
+ try:
+ kr = KgtkReader.open(kgtk_file,
error_file=error_file,
options=reader_options,
value_options=value_options,
@@ -115,7 +116,6 @@ def run(input_files: KGTKFiles,
very_verbose=very_verbose)
if header_only:
- kr.close()
if verbose:
print("Validated the header only.", file=error_file, flush=True)
else:
@@ -126,14 +126,22 @@ def run(input_files: KGTKFiles,
if verbose:
print("Validated %d data lines" % line_count, file=error_file, flush=True)
- if report_summary or verbose:
- print("\n====================================================", file=error_file, flush=True)
- kr.report_summary()
-
- return 0
+ except ValueError as e:
+ print("Error: %s" % str(e), file=error_file, flush=True)
+ raise KGTKException("Exiting due to error")
except SystemExit as e:
raise KGTKException("Exit requested")
except Exception as e:
raise KGTKException(str(e))
+ finally:
+ if kr is not None:
+ if report_summary or verbose:
+ print("\n====================================================", file=error_file, flush=True)
+ kr.report_summary()
+
+ kr.close()
+
+ return 0
+
