Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Jul 31, 2024
1 parent d2bd73b commit b412732
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/cfeintact/fisher_exact_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@


def default_fisher_exact(*args, **kwargs):
raise UserError("Cannot import scipy for fisher_exact test.")
raise UserError("Cannot import scipy for fisher_exact test. "
"Please check if you have enough RAM for scipy (python -c 'import scipy').")


try:
Expand All @@ -12,6 +13,7 @@ def default_fisher_exact(*args, **kwargs):
except ImportError as e:
logger.warning("Cannot import scipy: %s.", e)
fisher_exact = default_fisher_exact
except KeyboardInterrupt as e:
logger.warning("Cannot import scipy: %r.", e)
except KeyboardInterrupt:
logger.warning("Cannot import scipy for fisher_exact test. "
"Please check if you have enough RAM for scipy (python -c 'import scipy').")
fisher_exact = default_fisher_exact
2 changes: 1 addition & 1 deletion src/cfeintact/intact.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def __init__(self, working_dir, fmt):
self.subtypes = set()

if fmt not in ("json", "csv"):
raise UserError(f"Unrecognized output format {fmt!r}.")
raise UserError("Unrecognized output format %r.", fmt)

def __enter__(self, *args):
self.subtypes_file = open(self.subtypes_path, 'w')
Expand Down
4 changes: 2 additions & 2 deletions src/cfeintact/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def intact(input_file: str, subtype: str, check_packaging_signal: bool,
check_unknown_nucleotides, check_small_orfs, check_distance, output_csv,
)
except UserError as e:
logger.error("%s", e.message)
exit(1)
logger.fatal(e.fmt, *e.fmt_args)
exit(e.code)

exit(0)

Expand Down
8 changes: 5 additions & 3 deletions src/cfeintact/user_error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

class UserError(ValueError):
def __init__(self, message: str):
self.message = message
class UserError(RuntimeError):
def __init__(self, fmt: str, *fmt_args: object):
self.fmt = fmt
self.fmt_args = fmt_args
self.code = 1
10 changes: 6 additions & 4 deletions src/cfeintact/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ def mafft(sequences: Iterable[SeqRecord]) -> MultipleSeqAlignment:
subprocess.run(["mafft", "--quiet", alignment_input.name],
shell=False, stdout=alignment_output, check=True)
except Exception as e:
raise UserError(f"MAFFT run failed with {e}. "
f"Please check if the input .FASTA file is correctly formatted.") from e
raise UserError("MAFFT run failed with %s. "
"Please check if the input FASTA file is correctly formatted.",
e) from e

alignment: MultipleSeqAlignment = AlignIO.read(alignment_output.name, "fasta")
return alignment
Expand Down Expand Up @@ -60,8 +61,9 @@ def blast(alignment_file: str, input_file: str, output_file: str) -> None:
check=True,
)
except Exception as e:
raise UserError(f"BLAST run failed with {e}. "
f"Please check if the input .FASTA file is correctly formatted.") from e
raise UserError("BLAST run failed with %s. "
"Please check if the input FASTA file is correctly formatted.",
e) from e

output.write(','.join(fields) + '\n')
alignment_output.seek(0)
Expand Down

0 comments on commit b412732

Please sign in to comment.