From d499b9f75f7db970ba15815f800476ba4c4e9d63 Mon Sep 17 00:00:00 2001 From: uttam12331 Date: Mon, 29 Jun 2026 20:55:19 +0530 Subject: [PATCH] Expose ClickException and UsageError from the typer namespace After vendoring Click in 0.26.0, Typer re-exports several vendored exception types (BadParameter, Abort, Exit) but not the general-purpose ClickException / UsageError, leaving no public way to raise a concise user-facing CLI error without reaching into the private ._click module. Re-export ClickException and UsageError so apps can do `raise typer.ClickException("...")` and get Typer's error rendering and the expected exit code. Closes #1867 --- tests/test_exit_errors.py | 24 ++++++++++++++++++++++++ typer/__init__.py | 2 ++ 2 files changed, 26 insertions(+) diff --git a/tests/test_exit_errors.py b/tests/test_exit_errors.py index 1fdc3e36b0..c2b063061e 100644 --- a/tests/test_exit_errors.py +++ b/tests/test_exit_errors.py @@ -56,3 +56,27 @@ def main(): result = runner.invoke(app) assert result.exit_code == 1 + + +def test_click_exception(): + app = typer.Typer() + + @app.command() + def main(): + raise typer.ClickException("something broke") + + result = runner.invoke(app) + assert result.exit_code == 1 + assert "something broke" in result.output + + +def test_usage_error(): + app = typer.Typer() + + @app.command() + def main(): + raise typer.UsageError("bad usage") + + result = runner.invoke(app) + assert result.exit_code == 2 + assert "bad usage" in result.output diff --git a/typer/__init__.py b/typer/__init__.py index cbc8f36a1d..c35786c22f 100644 --- a/typer/__init__.py +++ b/typer/__init__.py @@ -7,7 +7,9 @@ from . import colors as colors from ._click.exceptions import Abort as Abort from ._click.exceptions import BadParameter as BadParameter +from ._click.exceptions import ClickException as ClickException from ._click.exceptions import Exit as Exit +from ._click.exceptions import UsageError as UsageError from ._click.termui import confirm as confirm from ._click.termui import getchar as getchar from ._click.termui import progressbar as progressbar