You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/api-guides/error-handling.rst
+47-46Lines changed: 47 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,13 +45,29 @@ Additionally, :cpp:func:`esp_err_to_name_r` function will attempt to interpret t
45
45
46
46
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.
47
47
48
-
49
48
.. _esp-error-check-macro:
50
49
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.
53
69
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.
55
71
56
72
Error message will typically look like this:
57
73
@@ -76,53 +92,40 @@ Error message will typically look like this:
76
92
- 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.
77
93
78
94
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
93
96
-----------------------------
94
97
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.
96
99
100
+
The macros are defined as follows:
97
101
98
-
.. _esp-goto-on-error-macro:
102
+
- **ESP_RETURN_ON_...**: Return from the function if an error or failed condition is detected:
99
103
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.
102
108
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:
104
110
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.
105
115
106
-
.. _esp-return-on-false-macro:
116
+
- **ESP_RETURN_VOID_...**: Return from a ``void`` function if an error or failed condition is detected:
107
117
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.
120
122
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.
121
124
122
125
.. _check_macros_examples:
123
126
124
-
``CHECK MACROS`` Examples
125
-
-------------------------
127
+
Error Handling Examples
128
+
-----------------------
126
129
127
130
Some examples
128
131
@@ -146,13 +149,6 @@ Some examples
146
149
return ret;
147
150
}
148
151
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
-
156
152
Error Handling Patterns
157
153
-----------------------
158
154
@@ -194,7 +190,7 @@ Error Handling Patterns
194
190
return err;
195
191
}
196
192
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.
198
194
199
195
Terminating the application in case of an error is usually undesirable behavior for middleware components, but is sometimes acceptable at application level.
0 commit comments