Skip to content

Commit 43e2aba

Browse files
feat(docs): Improve error handling documentation
Merges #15930
1 parent 2cc28d9 commit 43e2aba

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

docs/en/api-guides/error-handling.rst

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,29 @@ Additionally, :cpp:func:`esp_err_to_name_r` function will attempt to interpret t
4545

4646
This feature is enabled by default, but can be disabled to reduce application binary size. See :ref:`CONFIG_ESP_ERR_TO_NAME_LOOKUP`. When this feature is disabled, :cpp:func:`esp_err_to_name` and :cpp:func:`esp_err_to_name_r` are still defined and can be called. In this case, :cpp:func:`esp_err_to_name` will return ``UNKNOWN ERROR``, and :cpp:func:`esp_err_to_name_r` will return ``Unknown error 0xXXXX(YYYYY)``, where ``0xXXXX`` and ``YYYYY`` are the hexadecimal and decimal representations of the error code, respectively.
4747

48-
4948
.. _esp-error-check-macro:
5049

51-
``ESP_ERROR_CHECK`` Macro
52-
-------------------------
50+
Macro For Unrecoverable Errors
51+
------------------------------
52+
53+
The :c:macro:`ESP_ERROR_CHECK` macro, defined in ``esp_err.h``, is used to handle unrecoverable errors in ESP-IDF applications. It functions similarly to the standard ``assert`` macro, but specifically checks whether an :cpp:type:`esp_err_t` value is equal to :c:macro:`ESP_OK`. If the value is not :c:macro:`ESP_OK`, :c:macro:`ESP_ERROR_CHECK` prints a detailed error message and calls ``abort()``, terminating the application.
54+
55+
The behavior of :c:macro:`ESP_ERROR_CHECK` can be controlled using assertion-related configuration options:
56+
57+
- If ``CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE`` is set (default), the macro prints an error message and terminates the program.
58+
- If ``CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT`` is enabled, the program terminates silently without printing an error message.
59+
- If ``CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE`` (``NDEBUG`` is defined), the macro only prints an error message and does not terminate the program.
60+
61+
Use :c:macro:`ESP_ERROR_CHECK` in situations where an error is considered fatal and the application cannot continue safely. For situations where the application can recover from an error, use the macros described in the next section.
62+
63+
``ESP_ERROR_CHECK_WITHOUT_ABORT``
64+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
66+
The :c:macro:`ESP_ERROR_CHECK_WITHOUT_ABORT` macro, defined in ``esp_err.h``, is closely related to the **Macros For Recoverable Errors**. The macro behaves similarly to :c:macro:`ESP_ERROR_CHECK`, but instead of terminating the program with ``abort()``, it logs an error message in the same format and returns the error code if the value is not :c:macro:`ESP_OK`. This allows the application to continue running, making it suitable for cases where errors should be reported but are not considered fatal.
67+
68+
The behavior of :c:macro:`ESP_ERROR_CHECK_WITHOUT_ABORT` is controlled by the same assertion-related configuration options as :c:macro:`ESP_ERROR_CHECK`. If either ``CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE`` or ``CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT`` is enabled, the macro does not print any error message, otherwise, the macro prints an error message.
5369

54-
:c:macro:`ESP_ERROR_CHECK` macro serves similar purpose as ``assert``, except that it checks :cpp:type:`esp_err_t` value rather than a ``bool`` condition. If the argument of :c:macro:`ESP_ERROR_CHECK` is not equal :c:macro:`ESP_OK`, then an error message is printed on the console, and ``abort()`` is called.
70+
Use :c:macro:`ESP_ERROR_CHECK_WITHOUT_ABORT` when you want to log errors for diagnostic purposes without stopping the application.
5571

5672
Error message will typically look like this:
5773

@@ -76,53 +92,40 @@ Error message will typically look like this:
7692
- Finally, backtrace is printed. This is part of panic handler output common to all fatal errors. See :doc:`Fatal Errors <fatal-errors>` for more information about the backtrace.
7793

7894

79-
.. _esp-error-check-without-abort-macro:
80-
81-
``ESP_ERROR_CHECK_WITHOUT_ABORT`` Macro
82-
---------------------------------------
83-
84-
:c:macro:`ESP_ERROR_CHECK_WITHOUT_ABORT` macro serves similar purpose as ``ESP_ERROR_CHECK``, except that it will not call ``abort()``.
85-
86-
.. note::
87-
88-
Macros below require ``esp_check.h`` header file to be included
89-
90-
.. _esp-return-on-error-macro:
91-
92-
``ESP_RETURN_ON_ERROR`` Macro
95+
Macros For Recoverable Errors
9396
-----------------------------
9497

95-
:c:macro:`ESP_RETURN_ON_ERROR` macro checks the error code, if the error code is not equal :c:macro:`ESP_OK`, it prints the message and returns the error code.
98+
For recoverable errors, ESP-IDF provides a set of macros defined in ``esp_check.h``. The **ESP_RETURN_ON_...**, **ESP_GOTO_ON_...**, and **ESP_RETURN_VOID_ON_...** macros enable concise and consistent error handling, improving code readability and maintainability. Unlike ``ESP_ERROR_CHECK``, these macros do not terminate the program; instead, they print an error message and return or jump as appropriate. For use in interrupt service routines (ISRs), corresponding ``_ISR`` versions (such as :c:macro:`ESP_RETURN_ON_ERROR_ISR`) are available, ensuring safe operation in interrupt contexts.
9699

100+
The macros are defined as follows:
97101

98-
.. _esp-goto-on-error-macro:
102+
- **ESP_RETURN_ON_...**: Return from the function if an error or failed condition is detected:
99103

100-
``ESP_GOTO_ON_ERROR`` Macro
101-
---------------------------
104+
- :c:macro:`ESP_RETURN_ON_ERROR` - Checks an error code; if not :c:macro:`ESP_OK`, prints a message and returns the error code.
105+
- :c:macro:`ESP_RETURN_ON_FALSE` - Checks a condition; if false, prints a message and returns the supplied ``err_code``.
106+
- :c:macro:`ESP_RETURN_ON_ERROR_ISR` - For ISR context.
107+
- :c:macro:`ESP_RETURN_ON_FALSE_ISR` - For ISR context.
102108

103-
:c:macro:`ESP_GOTO_ON_ERROR` macro checks the error code, if the error code is not equal :c:macro:`ESP_OK`, it prints the message, sets the local variable ``ret`` to the code, and then exits by jumping to ``goto_tag``.
109+
- **ESP_GOTO_ON_...**: Jump to a label if an error or failed condition is detected:
104110

111+
- :c:macro:`ESP_GOTO_ON_ERROR` - Checks an error code; if not :c:macro:`ESP_OK`, prints a message, sets ``ret`` to the code, and jumps to ``goto_tag``.
112+
- :c:macro:`ESP_GOTO_ON_FALSE` - Checks a condition; if false, prints a message, sets ``ret`` to ``err_code``, and jumps to ``goto_tag``.
113+
- :c:macro:`ESP_GOTO_ON_ERROR_ISR` - For ISR context.
114+
- :c:macro:`ESP_GOTO_ON_FALSE_ISR` - For ISR context.
105115

106-
.. _esp-return-on-false-macro:
116+
- **ESP_RETURN_VOID_...**: Return from a ``void`` function if an error or failed condition is detected:
107117

108-
``ESP_RETURN_ON_FALSE`` Macro
109-
-----------------------------
110-
111-
:c:macro:`ESP_RETURN_ON_FALSE` macro checks the condition, if the condition is not equal ``true``, it prints the message and returns with the supplied ``err_code``.
112-
113-
114-
.. _esp-goto-on-false-macro:
115-
116-
``ESP_GOTO_ON_FALSE`` Macro
117-
---------------------------
118-
119-
:c:macro:`ESP_GOTO_ON_FALSE` macro checks the condition, if the condition is not equal ``true``, it prints the message, sets the local variable ``ret`` to the supplied ``err_code``, and then exits by jumping to ``goto_tag``.
118+
- :c:macro:`ESP_RETURN_VOID_ON_ERROR` - Checks an error code; if not :c:macro:`ESP_OK`, prints a message and returns.
119+
- :c:macro:`ESP_RETURN_VOID_ON_FALSE` - Checks a condition; if false, prints a message and returns.
120+
- :c:macro:`ESP_RETURN_VOID_ON_ERROR_ISR` - For ISR context.
121+
- :c:macro:`ESP_RETURN_VOID_ON_FALSE_ISR` - For ISR context.
120122

123+
The default behavior of these macros can be adjusted: if the :ref:`CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT` option is enabled in Kconfig, error messages will not be included in the application binary and will not be printed.
121124

122125
.. _check_macros_examples:
123126

124-
``CHECK MACROS`` Examples
125-
-------------------------
127+
Error Handling Examples
128+
-----------------------
126129

127130
Some examples
128131

@@ -146,13 +149,6 @@ Some examples
146149
return ret;
147150
}
148151
149-
.. note::
150-
151-
If the option :ref:`CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT` in Kconfig is enabled, the error message will be discarded, while the other action works as is.
152-
153-
The ``ESP_RETURN_XX`` and ``ESP_GOTO_xx`` macros cannot be called from ISR. While there are ``xx_ISR`` versions for each of them, e.g., ``ESP_RETURN_ON_ERROR_ISR``, these macros could be used in ISR.
154-
155-
156152
Error Handling Patterns
157153
-----------------------
158154

@@ -194,7 +190,7 @@ Error Handling Patterns
194190
return err;
195191
}
196192
197-
3. Convert into unrecoverable error, for example using ``ESP_ERROR_CHECK``. See `ESP_ERROR_CHECK macro`_ section for details.
193+
3. Convert into unrecoverable error, for example using ``ESP_ERROR_CHECK``. See `Macro For Unrecoverable Errors`_ section for details.
198194

199195
Terminating the application in case of an error is usually undesirable behavior for middleware components, but is sometimes acceptable at application level.
200196

@@ -211,3 +207,8 @@ C++ Exceptions
211207
--------------
212208

213209
See :ref:`cplusplus_exceptions`.
210+
211+
API Reference
212+
-------------
213+
214+
See :ref:`esp-check-api-ref`.

docs/en/api-reference/system/esp_err.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ For general information about error codes in ESP-IDF, see :doc:`Error Handling <
99

1010
For the full list of error codes defined in ESP-IDF, see :doc:`Error Codes Reference <../error-codes>`.
1111

12+
.. _esp-check-api-ref:
13+
1214
API Reference
1315
-------------
1416

0 commit comments

Comments
 (0)