Debian Codegraph @

context/error-handling.md

raw
1 # Error handling
2
3 ## Tracebacks
4
5 Do not manually generate tracebacks for errors or warnings.
6 Standardized traceback capture is included for errors, warnings and deprecation warnings for both controller and module code (Python).
7 See the `DISPLAY_TRACEBACK` config option for details on how to enable traceback capture and display.
8
9 ## In modules
10
11 In most cases, just raise an exception.
12 The AnsiballZ wrapper now provides a general exception handler for Python modules, making use of `fail_json` unnecessary, unless the module result needs to be customized.
13 Calls to the various Python-module-side `warn` and `deprecate` methods/functions will also capture and marshal tracebacks to the controller when enabled.
14 When using `fail_json` to customize a module failure result, passing the `exception` argument to provide the currently active exception is unnecessary.
15
16 ## Deferred exceptions in modules
17
18 When deferring exceptions in modules using `try/except` and `fail_json`, pass the captured `Exception` instance to `fail_json` using the `exception` argument.
19 The error handling infrastructure will handle collection of error details and traceback formatting.
20
21 ## Exception context
22
23 Raising exceptions while another exception is active will cause the active exception to become the `__context__` for the newly raised exception.
24 This is usually not the intended behavior, and should be reserved for unexpected errors while handling the original exception.
25 In most cases a `raise from` is desired, either with `None` to suppress the original exception if it's not helpful,
26 or with the captured exception to set it as the `__cause__` for the newly raised exception.
27
28 Suppression example: `raise Exception("something") from None`
29
30 Cause example: `raise Exception("something") from ex`
31
32 ## Raising new exceptions
33
34 Don't catch exceptions just to re-raise them, unless there's additional information that can be added in the newly raised exception.
35 In most cases, particularly for plugin/module failures, contextual information is automatically added,
36 making granular `try/except/raise` within the plugin or module unnecessary.
37
38 ## Error messages
39
40 Do not repeat previous exception messages when constructing new exceptions.
41
42 Anti-pattern: `raise Exception("it broke: {ex}") from ex`
43
44 The built-in error chain handling mechanisms in Ansible will include the messages from cause/context exceptions automatically.
45
46 In general, error messages passed to exceptions should be a fairly terse description of what happened,
47 and not contain extra diagnostic, contextual, or prescriptive correction advice (see `obj` and `help_text` below for more on that).
48
49 ## When and how to use AnsibleError
50
51 The `AnsibleError` exception type provides support for improved error reporting.
52 However, if no arguments other than a message are given, there's usually no benefit over using built-in exception types.
53 So what are those other arguments?
54
55 - `obj` - Usually a variable responsible for the error being raised -- not an `Exception` instance.
56 If this value is `Origin` tagged, then the error message shown to the user will be able to provide context showing what content triggered the error.
57 - `help_text` - Instructions and additional detail that helps the user understand how to resolve the error.
58 By putting that information here, it allows the `message` to be shorter and focus on the problem.
59 This information will be shown *after* the contextual error details provided by `obj`, if any.
60
61 ## Display warnings and errors
62
63 The existing `warning` and `deprecated` methods on the `Display` object now support passing optional `help_text` and `obj` arguments,
64 matching those on `AnsibleError` for prescriptive guidance and source context from `Origin`-tagged values.
65 Additionally, the new `error_as_warning` method accepts an exception object and an optional contextual message directly,
66 allowing for a caught exception to be converted to a warning automatically
67 while still preserving the exception detail, traceback, and source object context (where applicable).
68
69 ## Jinja plugin errors
70
71 In Jinja plugins, the `AnsibleFilterError` and `AnsibleLookupError` exception types are no longer needed.
72 Instead, use whatever exception type is appropriate for the error condition.