Skip to content
Myr

exceptions.rst

This file

Path

docs/exceptions.rst

73 lines · 2.9 KiB

Last changed by

Not known.

History of this file

Take it

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

Merge pull request #1092 from sirosen/docfix-exampleDavid Lordbefore adoptiona0e0328

lines 1–73why, 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-------------------------1213Click's main error handling is happening in :meth:`BaseCommand.main`. In14there it handles all subclasses of :exc:`ClickException` as well as the15standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The16latter are internally translated into a :exc:`Abort`.1718The logic applied is the following:19201. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it21 as :exc:`Abort`.222. If an :exc:`ClickException` is raised, invoke the23 :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``.274. if it goes through well, exit the program with exit code ``0``.2829What 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.