Skip to content
Myr

exceptions.rst

This file

Path

docs/exceptions.rst

73 lines · 2.9 KiB

Last changed by

deprecate base classes `BaseCommand` and `MultiCommand` (#2591)David Lordbefore adoptionedcd2dc

History of this file

Take it

The walk stopped after 200 commits: lines older than ddcabdd point at it.

Merge pull request #1991 from pallets/pre-commit-ci-update-configDavid Lordbefore adoptionddcabdd

lines 1–12why, one step back
1Exception Handling
2==================34.. currentmodule:: click56Click internally uses exceptions to signal various error conditions that7the user of the application might have caused. Primarily this is things8like incorrect usage.910Where are Errors Handled?11-------------------------12

deprecate base classes `BaseCommand` and `MultiCommand` (#2591)David Lordbefore adoptionedcd2dc

line 13why, one step back
13Click's main error handling is happening in :meth:`Command.main`. In

Merge pull request #1991 from pallets/pre-commit-ci-update-configDavid Lordbefore adoptionddcabdd

lines 14–15why, one step back
14there it handles all subclasses of :exc:`ClickException` as well as the
15standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 16why, one step back
16latter are internally translated into an :exc:`Abort`.

Merge pull request #1991 from pallets/pre-commit-ci-update-configDavid Lordbefore adoptionddcabdd

lines 17–21why, one step back
17
18The logic applied is the following:19201. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it21 as :exc:`Abort`.

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 22why, one step back
222. If a :exc:`ClickException` is raised, invoke the

Merge pull request #1991 from pallets/pre-commit-ci-update-configDavid Lordbefore adoptionddcabdd

lines 23–26why, one step back
23 :meth:`ClickException.show` method on it to display it and then exit
24 the program with :attr:`ClickException.exit_code`.253. If an :exc:`Abort` exception is raised print the string ``Aborted!``26 to standard error and exit the program with exit code ``1``.

Merge branch '8.1.x'David Lordbefore adoption5e75fe7

line 27why, one step back
274. If it goes through well, exit the program with exit code ``0``.

Merge pull request #1991 from pallets/pre-commit-ci-update-configDavid Lordbefore adoptionddcabdd

lines 28–73why, one step back
28
29What if I don't want that?30--------------------------3132Generally you always have the option to invoke the :meth:`invoke` method33yourself. For instance if you have a :class:`Command` you can invoke it34manually like this::3536 ctx = command.make_context('command-name', ['args', 'go', 'here'])37 with ctx:38 result = command.invoke(ctx)3940In this case exceptions will not be handled at all and bubbled up as you41would expect.4243Starting with Click 3.0 you can also use the :meth:`Command.main` method44but disable the standalone mode which will do two things: disable45exception handling and disable the implicit :meth:`sys.exit` at the end.4647So you can do something like this::4849 command.main(['command-name', 'args', 'go', 'here'],50 standalone_mode=False)5152Which Exceptions Exist?53-----------------------5455Click has two exception bases: :exc:`ClickException` which is raised for56all exceptions that Click wants to signal to the user and :exc:`Abort`57which is used to instruct Click to abort the execution.5859A :exc:`ClickException` has a :meth:`~ClickException.show` method which60can render an error message to stderr or the given file object. If you61want to use the exception yourself for doing something check the API docs62about what else they provide.6364The following common subclasses exist:6566* :exc:`UsageError` to inform the user that something went wrong.67* :exc:`BadParameter` to inform the user that something went wrong with68 a specific parameter. These are often handled internally in Click and69 augmented with extra information if possible. For instance if those70 are raised from a callback Click will automatically augment it with71 the parameter name if possible.72* :exc:`FileError` this is an error that is raised by the73 :exc:`FileType` if Click encounters issues opening the file.