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
Merge branch 'contrib/github_pr_15930' into 'master'
docs(esp32): Add note about including esp_check.h for error-handling.rst docs page in English (GitHub PR)
Closes IDFGH-15271 and DOC-11245
See merge request espressif/esp-idf!39012
Copy file name to clipboardExpand all lines: docs/en/api-guides/error-handling.rst
+48-44Lines changed: 48 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Identifying and handling run-time errors is important for developing robust appl
21
21
- CPU exceptions: access to protected regions of memory, illegal instruction, etc.
22
22
- System level checks: watchdog timeout, cache access error, stack overflow, stack smashing, heap corruption, etc.
23
23
24
-
This guide explains ESP-IDF error handling mechanisms related to recoverable errors, and provides some common error handling patterns.
24
+
This guide primarily introduces the error handling mechanisms in ESP-IDF for **recoverable errors** and provides common error handling patterns. Some sections also mention macros used for **unrecoverable errors**, with the aim of illustrating their use in scenarios with different levels of error severity.
25
25
26
26
For instructions on diagnosing unrecoverable errors, see :doc:`Fatal Errors <fatal-errors>`.
27
27
@@ -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 prints 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.
53
67
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.
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.
69
+
70
+
Use :c:macro:`ESP_ERROR_CHECK_WITHOUT_ABORT` when you want to print errors for diagnostic purposes without stopping the application.
55
71
56
72
Error message will typically look like this:
57
73
@@ -76,50 +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
-
87
-
.. _esp-return-on-error-macro:
88
-
89
-
``ESP_RETURN_ON_ERROR`` Macro
95
+
Macros For Recoverable Errors
90
96
-----------------------------
91
97
92
-
: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.
93
99
100
+
The macros are defined as follows:
94
101
95
-
.. _esp-goto-on-error-macro:
102
+
- **ESP_RETURN_ON_...**: Return from the function if an error or failed condition is detected:
96
103
97
-
``ESP_GOTO_ON_ERROR`` Macro
98
-
---------------------------
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.
99
108
100
-
: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:
101
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.
102
115
103
-
.. _esp-return-on-false-macro:
116
+
- **ESP_RETURN_VOID_...**: Return from a ``void`` function if an error or failed condition is detected:
104
117
105
-
``ESP_RETURN_ON_FALSE`` Macro
106
-
-----------------------------
107
-
108
-
: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``.
109
-
110
-
111
-
.. _esp-goto-on-false-macro:
112
-
113
-
``ESP_GOTO_ON_FALSE`` Macro
114
-
---------------------------
115
-
116
-
: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.
117
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.
118
124
119
125
.. _check_macros_examples:
120
126
121
-
``CHECK MACROS`` Examples
122
-
-------------------------
127
+
Error Handling Examples
128
+
-----------------------
123
129
124
130
Some examples
125
131
@@ -143,13 +149,6 @@ Some examples
143
149
return ret;
144
150
}
145
151
146
-
.. note::
147
-
148
-
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.
149
-
150
-
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.
151
-
152
-
153
152
Error Handling Patterns
154
153
-----------------------
155
154
@@ -191,7 +190,7 @@ Error Handling Patterns
191
190
return err;
192
191
}
193
192
194
-
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.
195
194
196
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