ESP_GOTO_ON_ERROR modification idea

greg-dickson
Posts: 24
Joined: Sun Nov 01, 2020 1:51 am

ESP_GOTO_ON_ERROR modification idea

Postby greg-dickson » Sat May 27, 2023 12:25 am

As the existing code takes format parameters but doesn't use them and also insists that the error variable is defined as ret I had a play and came up with this.
Feel free to modify it test it etc

Code: Select all

#define GOTO_ON_ERROR(x, goto_tag, log_tag, error_var, format, ...) do {        \
        (void)log_tag;                                                          \
        error_var = (x);                                                        \
        if (error_var != ESP_OK) {                                              \
                ESP_LOGE(log_tag, "%s(%d): Error code is: %d, " format, __FUNCTION__, __LINE__, error_var, __VA_ARGS__); \
            goto goto_tag;                                                      \
        }                                                                       \
    } while(0)

it can be called with something like

Code: Select all

GOTO_ON_ERROR(i2c_driver_install(i2c_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0), i2c_init_end, "I2C Truc", _error, "Error calling i2c_driver_install (%d, %d, %d, %d %d)",i2c_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);


this only requires the inclusion of "esp_log.h"
this will actually print the error message on error and the error code as well
it takes the error variable as an argument and as such breaks the existing code

Anyway just my 2 cents for now.

Who is online

Users browsing this forum: ok-home and 79 guests