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 fileTake it
The walk stopped after 200 commits: lines older than 91b2cd6 point at it.
unpin pytestDavid Lordbefore adoption91b2cd6
lines 1–12why, one step back1Exception Handling2==================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-------------------------12deprecate base classes `BaseCommand` and `MultiCommand` (#2591)David Lordbefore adoptionedcd2dc
line 13why, one step back13Click's main error handling is happening in :meth:`Command.main`. Inunpin pytestDavid Lordbefore adoption91b2cd6
lines 14–15why, one step back14there it handles all subclasses of :exc:`ClickException` as well as the15standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. TheMerge branch '8.1.x'David Lordbefore adoption5e75fe7
line 16why, one step back16latter are internally translated into an :exc:`Abort`.unpin pytestDavid Lordbefore adoption91b2cd6
lines 17–21why, one step back1718The 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 back222. If a :exc:`ClickException` is raised, invoke theunpin pytestDavid Lordbefore adoption91b2cd6
lines 23–26why, one step back23 :meth:`ClickException.show` method on it to display it and then exit24 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 back274. If it goes through well, exit the program with exit code ``0``.unpin pytestDavid Lordbefore adoption91b2cd6
lines 28–73why, one step back2829What 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.