Skip to content
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

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

Open
wants to merge 1 commit 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
12 changes: 12 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,18 @@ 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.
*/
#define ESP_RETURN_ON_ERR(x, ...) do { \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest renaming it as the macros in this file start with ESP_ERROR_CHECK_ . It could be something like ESP_ERROR_RETURN, or ESP_ERROR_CHECK_RETURN.

Additionally, it would be good to have an example of usage. Could you add in the description of the macro how to use it, something like this, I guess:

 * @example
 * esp_err_t get_data()
 * {
 *     ESP_ERROR_CHECK_RETURN(read_file())
 *     ESP_ERROR_CHECK_RETURN(read_file(), close_file())
 *     return ESP_OK;
 * }

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