Skip to content

feat: Add ESP_RETURN_ON_ERR macro (IDFGH-14787) #15525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions components/esp_common/include/esp_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ void _esp_error_check_failed_without_abort(esp_err_t rc, const char *file, int l
#endif
/** @endcond */

/**
* Macro that returns the error code if an error occurs, optionally running extra code.
* The extra code can be placed after the function thay may error and it will only run if the error occurs.
*
* @example
* esp_err_t my_function()
* {
* // Let's say `read_file()` returns esp_err_t and we want to `close_file()` on error.
* ESP_ERROR_CHECK_RETURN(read_file(), close_file())
* // If the code page gets here, `read_file()` succeeded.
* process_file();
* close_file();
* return ESP_OK;
* }
*/
#define ESP_ERROR_CHECK_RETURN(x, ...) do { \
esp_err_t err_rc_ = (x); \
if (unlikely(err_rc_ != ESP_OK)) { \
__VA_ARGS__; \
return err_rc_; \
} \
} while (0)

/**
* Macro which can be used to check the error code,
* and terminate the program in case the code is not ESP_OK.
Expand Down