Skip to content

Commit 6355691

Browse files
authored
Merge pull request #169 from rok/uniform-errors
REFACTOR: unify error hierarchy and centralize CLI error and store setup
2 parents 0bbdf41 + 221a316 commit 6355691

11 files changed

Lines changed: 196 additions & 265 deletions

File tree

‎src/benchx/cli.py‎

Lines changed: 139 additions & 255 deletions
Large diffs are not rendered by default.

‎src/benchx/config.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from . import adapters, core
2929
from .core import loader
30+
from .errors import BenchxError
3031

3132
CONFIG_FILE = "benchx.conf.json"
3233
# Files of the format this one replaced. They are not read; finding one is an error, so
@@ -59,7 +60,7 @@ def _default_store_in(directory: Path) -> Path:
5960
return parquet_path if _has_pyarrow() else jsonl_path
6061

6162

62-
class ConfigError(Exception):
63+
class ConfigError(BenchxError):
6364
"""The configuration is unusable, or a required value is missing; the message says why."""
6465

6566

‎src/benchx/errors.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
"""Errors shared by the workbench commands (bx bench, bx compare)."""
1+
"""User-facing errors shared across benchx.
22
3+
Every operational or usage refusal subclasses `BenchxError` so the CLI can
4+
report it uniformly (`bx <cmd>: error: <message>`) and exit with code 2.
5+
"""
36

4-
class SessionError(Exception):
7+
8+
class BenchxError(Exception):
9+
"""Base class for user-facing refusals and configuration/usage errors."""
10+
11+
12+
class UsageError(BenchxError):
13+
"""Invalid or conflicting command-line flags, arguments, or environment variables."""
14+
15+
16+
class SessionError(BenchxError):
517
"""The session cannot start or finish; the message says why."""

‎src/benchx/evidence.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import Any
1414

1515
from .core import canonical
16+
from .errors import BenchxError
1617
from .key import Key
1718

1819
SIDE = "side"
@@ -28,7 +29,7 @@
2829
}
2930

3031

31-
class CompareError(Exception):
32+
class CompareError(BenchxError):
3233
"""The request itself cannot be answered, such as an unknown baseline."""
3334

3435

‎src/benchx/orders.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
from datetime import UTC, datetime
1111

1212
from . import core
13+
from .errors import BenchxError
1314

1415
SCHEMA_VERSION = core.catalog.WORK_ORDER
1516
REQUESTER = {"kind": "workbench", "name": "bx"}
1617
CASE_TIMEOUT_S, ORDER_TIMEOUT_S = 60, 600
1718

1819

19-
class OrderError(Exception):
20+
class OrderError(BenchxError):
2021
"""The order does not satisfy its schema or rules; the message says why."""
2122

2223

‎src/benchx/projection.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from .core import sha256_hex
1818
from .core.hashing import canonical
19+
from .errors import BenchxError
1920
from .key import Key, axis_order, canonical_uri, escape, get, normalize_remote # noqa: F401
2021

2122
# The reported coordinates (schema §4.3), which every role applies to, and the
@@ -26,7 +27,7 @@
2627
CODE = ("/revision/key", "/provenance/subject_tree")
2728

2829

29-
class ProjectionError(Exception):
30+
class ProjectionError(BenchxError):
3031
"""A projection that breaks a rule of §4.3."""
3132

3233

‎src/benchx/target.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from . import core, log
2626
from .core import loader
27+
from .errors import BenchxError
2728

2829
logger = logging.getLogger(__name__)
2930

@@ -32,7 +33,7 @@
3233
MAX_FACTS_CHARS = 65536 # how_built as an object, serialized; as text it is limited to 8192
3334

3435

35-
class TargetError(Exception):
36+
class TargetError(BenchxError):
3637
"""The target cannot be described; the message says why."""
3738

3839

‎src/benchx/views/inputs.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import rfc8785
2323

2424
from .. import identity, projection
25+
from ..errors import BenchxError
2526
from ..results import MemoryStore
2627

2728
UTC = dt.UTC
@@ -31,7 +32,7 @@
3132
_DOTS_RUN_RE = re.compile(r"\.\.+")
3233

3334

34-
class ViewError(Exception):
35+
class ViewError(BenchxError):
3536
"""The view inputs could not be read or built."""
3637

3738

‎tests/test_logging.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_the_console_handler_writes_a_multi_line_message_verbatim(capsys):
306306
def test_the_log_file_is_left_out_of_every_tree_identity(build, tmp):
307307
path = tmp / "wt.log"
308308
args = argparse.Namespace(log=str(path), log_level=None, argv=[], verbosity=0, verbosity_source="default", out=None)
309-
cfg = cli._load("bench")
309+
cfg = cli._load()
310310
try:
311311
started = cli._start_logging(cfg, args, None, "bench")
312312
assert started.log_file == path.resolve() and path.resolve() in snapshot._IGNORED

‎tests/test_refusals_not_crashes.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,32 @@ def test_missing_format_checker_in_custom_schemas_is_refused(tmp_path, adhoc, mo
241241
assert "bx compare: error:" in err and "no-such-custom-format" in err and "Traceback" not in err
242242
finally:
243243
catalog.clear_cache()
244+
245+
246+
# ---- uniform error hierarchy and CLI refusal formatting
247+
248+
249+
def test_domain_errors_subclass_benchx_error():
250+
from benchx import config, evidence, orders, projection, target, views
251+
from benchx.errors import BenchxError, SessionError, UsageError
252+
253+
for exc_type in (
254+
UsageError,
255+
SessionError,
256+
config.ConfigError,
257+
orders.OrderError,
258+
target.TargetError,
259+
evidence.CompareError,
260+
projection.ProjectionError,
261+
views.ViewError,
262+
):
263+
assert issubclass(exc_type, BenchxError)
264+
265+
266+
def test_compare_and_target_refusals_use_uniform_cli_error_prefix(tmp_path, monkeypatch, capsys):
267+
monkeypatch.chdir(tmp_path)
268+
assert cli.main(["compare", "--run", "missing", "--profile", "revisions", "--baseline", "abc"]) == cli.EXIT_REFUSED
269+
assert "bx compare: error: no results for run 'missing'" in capsys.readouterr().err
270+
271+
assert cli.main(["target", "describe", "@missing"]) == cli.EXIT_REFUSED
272+
assert 'bx target: error: @missing needs "provider"' in capsys.readouterr().err

0 commit comments

Comments
 (0)