Added exception docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 6a559ec · Browse files · 1 file·+73 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+73 added−0 removed
- other:+73 added−0 removed
Provenance
- Commit
36112b1- Tree
9e0c560- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6a559ec
1 file changed+73 added−0 removed
added:docs/exceptions.rst+73 added
1+Exception Handling2+==================3+4+.. currentmodule:: click5+6+Click internally uses exceptions to signal various error conditions that7+the user of the application might have caused. Primarily this is things8+like incorrect usage.9+10+Where are Errors Handled?11+-------------------------12+13+Click's main error handling is happening in :meth:`BaseCommand.main`. In14+there it handles all subclasses of :exc:`ClickException` as well as the15+standard :exc:`EOFError` and :exc:`KeyboardInterrupt` exceptions. The16+latter are internally translated into a :exc:`Abort`.17+18+The logic applied is the following:19+20+1. If an :exc:`EOFError` or :exc:`KeyboardInterrupt` happens, reraise it21+ as :exc:`Abort`.22+2. 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`.25+3. If an :exc:`Abort` exception is raised print the string ``Aborted!``26+ to standard error and exit the program with exit code ``1``.27+4. if it goes through well, exit the program with exit code ``0``.28+29+What if I don't want that?30+--------------------------31+32+Generally you always have the option to invoke the :meth:`invoke` method33+yourself. For instance if you have a :class:`Command` you can invoke it34+manually like this::35+36+ ctx = command.make_context('command-name', ['args', 'go', 'here'])37+ with ctx:38+ result = command.invoke(ctx)39+40+In this case exceptions will not be handled at all and bubbled up as you41+would expect.42+43+Starting with Click 3.0 you can also use the :meth:`Command.main` method44+but disable the standalone mode which will do two things: disable45+exception handling and disable the implicit :meth:`sys.exit` at the end.46+47+So you can do something like this::48+49+ command.main(['command-name', 'args', 'go', 'here'],50+ standalone_mode=False)51+52+Which Exceptions Exist?53+-----------------------54+55+Click has two exception bases: :exc:`ClickException` which is raised for56+all exceptions that Click wants to signal to the user and :exc:`Abort`57+which is used to instruct Click to abort the execution.58+59+A :exc:`ClickException` has a :meth:`~ClickException.show` method which60+can render an error message to stderr or the given file object. If you61+want to use the exception yourself for doing something check the API docs62+about what else they provide.63+64+The following common subclasses exist:65+66+* :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.