Skip to content

Commit 57756ed

Browse files
committed
🐛 raise CLI service errors directly
1 parent 412b49c commit 57756ed

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, NoReturn
44

55
import typer
66

@@ -10,7 +10,7 @@
1010
SERVICE_ERROR_EXIT_CODE = 1
1111

1212

13-
def exit_for_service_error(exc: ServiceError) -> typer.Exit:
14-
"""Render a service-level error and return a CLI exit exception."""
13+
def exit_for_service_error(exc: ServiceError) -> NoReturn:
14+
"""Render a service-level error and exit the CLI."""
1515
typer.echo(exc.msg, err=True)
16-
return typer.Exit(code=SERVICE_ERROR_EXIT_CODE)
16+
raise typer.Exit(code=SERVICE_ERROR_EXIT_CODE)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Tests for CLI error handling."""
2+
3+
import pytest
4+
import typer
5+
6+
from schnee.controllers.cli.errors import (
7+
SERVICE_ERROR_EXIT_CODE,
8+
exit_for_service_error,
9+
)
10+
from schnee.services.base import ServiceError
11+
12+
13+
class ExampleServiceError(ServiceError):
14+
"""Service error used by CLI tests."""
15+
16+
msg = "example service failure"
17+
18+
19+
def test_exit_for_service_error_writes_error_and_exits(
20+
capsys: pytest.CaptureFixture[str],
21+
) -> None:
22+
"""Service errors are rendered to stderr and terminate the CLI."""
23+
with pytest.raises(typer.Exit) as exc_info:
24+
exit_for_service_error(ExampleServiceError())
25+
26+
captured = capsys.readouterr()
27+
28+
assert exc_info.value.exit_code == SERVICE_ERROR_EXIT_CODE, (
29+
"service errors should terminate with the configured CLI error code"
30+
)
31+
assert ExampleServiceError.msg in captured.err, (
32+
"service errors should be rendered to stderr before exiting"
33+
)

0 commit comments

Comments
 (0)