Skip to content

Commit

Permalink
(HP-1728) log message when raising errors
Browse files Browse the repository at this point in the history
  • Loading branch information
george42-ctds committed Jan 21, 2025
1 parent 58749a6 commit f9e5d59
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
3 changes: 1 addition & 2 deletions heal/cli/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ def extract(input_file, output_dir):
try:
vlmd_extract(input_file, output_dir=output_dir)
except Exception as e:
logging.error(str(e))
raise e
logging.error(f"Extraction error {str(e)}")
4 changes: 2 additions & 2 deletions heal/cli/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def validate(input_file):
try:
vlmd_validate(input_file)
except Exception as e:
logging.error(str(e))
raise e
logging.error(f"Validation error {str(e)}")

logging.info("Valid")
18 changes: 12 additions & 6 deletions heal/vlmd/extract/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,25 @@ def vlmd_extract(

file_suffix = Path(input_file).suffix.replace(".", "")
if file_suffix not in ALLOWED_INPUT_TYPES:
raise ExtractionError(f"Input file must be one of {ALLOWED_INPUT_TYPES}")
message = f"Input file must be one of {ALLOWED_INPUT_TYPES}"
logger.error(message)
raise ExtractionError(message)
if not isfile(input_file):
raise ExtractionError(f"Input file does not exist: {input_file}")
message = f"Input file does not exist: {input_file}"
logger.error(message)
raise ExtractionError(message)

if file_type not in ALLOWED_FILE_TYPES:
raise ExtractionError(f"File type must be one of {ALLOWED_FILE_TYPES}")
message = f"File type must be one of {ALLOWED_FILE_TYPES}"
logger.error(message)
raise ExtractionError(message)
if file_type == "auto":
file_type = file_suffix

if output_type not in ALLOWED_OUTPUT_TYPES:
raise ExtractionError(
f"Unrecognized output_type '{output_type}' - should be in {ALLOWED_OUTPUT_TYPES}"
)
message = f"Unrecognized output_type '{output_type}' - should be in {ALLOWED_OUTPUT_TYPES}"
logger.error(message)
raise ExtractionError(message)

# validate
try:
Expand Down
36 changes: 24 additions & 12 deletions heal/vlmd/validate/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,29 @@ def vlmd_validate(
)
file_suffix = Path(input_file).suffix.replace(".", "")
if file_suffix not in ALLOWED_INPUT_TYPES:
raise ValueError(f"Input file must be one of {ALLOWED_INPUT_TYPES}")
message = f"Input file must be one of {ALLOWED_INPUT_TYPES}"
logger.error(message)
raise ValueError(message)
if not isfile(input_file):
raise IOError(f"Input file does not exist: {input_file}")
message = f"Input file does not exist: {input_file}"
logger.error(message)
raise IOError(message)

if schema_type not in ALLOWED_SCHEMA_TYPES:
raise ValueError(f"Schema type must be in {ALLOWED_SCHEMA_TYPES}")
message = f"Schema type must be in {ALLOWED_SCHEMA_TYPES}"
logger.error(message)
raise ValueError(message)
schema = get_schema(input_file, schema_type)
if schema is None:
raise ValueError(f"Could not get schema for type = {schema_type}")
message = f"Could not get schema for type = {schema_type}"
logger.error(message)
raise ValueError(message)

output_type = output_type if output_type else "json"
if output_type not in ALLOWED_OUTPUT_TYPES:
raise ValueError(
f"Unrecognized output_type '{output_type}' - should be in {ALLOWED_OUTPUT_TYPES}"
)
message = f"Unrecognized output_type '{output_type}' - should be in {ALLOWED_OUTPUT_TYPES}"
logger.error(message)
raise ValueError(message)

# TODO: We need this for csv - see if we can add this to get_schema
if file_suffix in ["csv", "tsv"]:
Expand All @@ -89,7 +97,9 @@ def vlmd_validate(
logger.debug("Getting csv data from file")
data = read_delim(input_file).to_dict(orient="records")
if len(data) == 0:
raise ValidationError("Could not read csv data from input")
message = "Could not read csv data from input"
logger.error(message)
raise ValidationError(message)
elif file_suffix == "json":
logger.debug("Getting json data from file")
data = read_data_from_json_file(input_file)
Expand All @@ -106,9 +116,9 @@ def vlmd_validate(
# convert
input_type = file_type_to_fxn_map.get(file_suffix)
if not input_type:
raise ExtractionError(
f"Could not get conversion function from file_suffix '{file_suffix}'"
)
message = f"Could not get conversion function from file_suffix '{file_suffix}'"
logger.error(message)
raise ExtractionError(message)
data_dictionaries = {}
logger.debug(f"Verifying vlmd can be converted using input_type '{input_type}'")
data_dictionary_props = {}
Expand Down Expand Up @@ -139,7 +149,9 @@ def vlmd_validate(
# TODO: see if we can add this to get_schema
schema = add_types_to_props(schema)
if schema is None:
raise ValueError(f"Could not get schema for type = {schema_type}")
message = f"Could not get schema for type = {schema_type}"
logger.error(message)
raise ValueError(message)

try:
jsonschema.validate(instance=converted_dictionary, schema=schema)
Expand Down

0 comments on commit f9e5d59

Please sign in to comment.