Skip to content

Commit

Permalink
Merge pull request #296 from zmoon/exc-type
Browse files Browse the repository at this point in the history
CLI: Include exception type name in non-debug message
  • Loading branch information
zmoon authored Nov 4, 2024
2 parents ae42d6d + a9dbf74 commit bc44e39
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion melodies_monet/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
""".strip()


def _get_full_name(obj):
"""Get the full name of a function or type,
including the module name if not builtin."""
import builtins
import inspect

mod = inspect.getmodule(obj)
name = obj.__qualname__
if mod is None or mod is builtins:
return name
else:
return f"{mod.__name__}.{name}"


@contextmanager
def _timer(desc=""):
start = time.perf_counter()
Expand All @@ -48,7 +62,7 @@ def _timer(desc=""):
tpl.format(status="failed", elapsed=time.perf_counter() - start),
fg=ERROR_COLOR
)
typer.secho(f"Error message: {e}", fg=ERROR_COLOR)
typer.secho(f"Error message (type: {_get_full_name(type(e))}): {e}", fg=ERROR_COLOR)
if DEBUG:
raise
else:
Expand Down
28 changes: 28 additions & 0 deletions melodies_monet/tests/test_get_data_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@
ds0_airnow = xr.open_dataset(fetch_example("airnow:2019-09"))


def test_get_aeronet_no_data_err():
cmd = [
"melodies-monet",
"get-aeronet",
"-s", "2100-01-01", # future
"-e", "2100-01-02",
]
cp = subprocess.run(cmd, capture_output=True)
assert cp.returncode != 0
assert cp.stdout.decode().splitlines()[-2].startswith(
"Error message (type: Exception): loading from URL 'https://aeronet.gsfc.nasa.gov/"
)


def test_get_aeronet_empty_date_range_err():
cmd = [
"melodies-monet",
"get-aeronet",
"-s", "2019-09-01",
"-e", "2019-08-31",
]
cp = subprocess.run(cmd, capture_output=True)
assert cp.returncode != 0
assert cp.stdout.decode().splitlines()[-2] == (
"Error message (type: ValueError): Neither `start` nor `end` can be NaT"
)


def test_get_aeronet(tmp_path):
fn = "x.nc"
cmd = [
Expand Down

0 comments on commit bc44e39

Please sign in to comment.