Skip to content

Commit d5fc733

Browse files
edoyangomicaeljtoliveira
authored andcommitted
raise similar invalid input error
1 parent 573c089 commit d5fc733

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/access/parsers/payujson_profiling.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,24 @@ def read(self, stream: str) -> dict:
5252
dict: Parsed timing information.
5353
5454
Raises:
55-
KeyError: when "timings" key is missing in input JSON.
56-
ValueError: if stream is not a string with valid JSON.
55+
ValueError: when input stream is not valid Payu JSON output.
5756
"""
57+
errmsg = "No Payu profiling data found"
5858

5959
try:
6060
timings = json.loads(stream)["timings"]
61-
except KeyError:
62-
raise KeyError('"timings" key missing in stream.')
63-
except json.JSONDecodeError:
64-
raise ValueError("Invalid JSON supplied.")
61+
except:
62+
raise ValueError(errmsg)
6563

6664
# remove known keys not relevant to profiling
6765
for unwanted_key in ("payu_start_time", "payu_finish_time"):
6866
if unwanted_key in timings:
6967
del timings[unwanted_key]
7068

69+
# error if no relevant keys in timings
70+
if not timings:
71+
raise ValueError(errmsg)
72+
7173
result = {"region": [], "walltime": []}
7274

7375
# transpose dict to be consistent with other profiling parsers.

tests/test_payujson_profiling.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ def test_payujson_profiling(payujson_parser, payujson_log_file, payujson_profili
6262
), f"Incorrect walltime for region {region} (idx: {idx})."
6363

6464

65-
def test_payujson_errors(payujson_parser):
65+
def test_payujson_incorrect_profiling(payujson_parser):
6666
"""Test that exceptions get raised appropriately."""
67-
with pytest.raises(KeyError):
67+
# missing "timings" key
68+
with pytest.raises(ValueError):
6869
payujson_parser.read('{"a": 123}')
70+
# empty "timings" value
71+
with pytest.raises(ValueError):
72+
payujson_parser.read('{"timings": {"payu_start_time": "2025-09-16T08:52:50.748807"}}')
73+
# invalid JSON altogether
6974
with pytest.raises(ValueError):
7075
payujson_parser.read("abc def")

0 commit comments

Comments
 (0)