Skip to content

Commit

Permalink
mx_mysql: Change error handling
Browse files Browse the repository at this point in the history
Currently, the functions in mx_mysql log errors from mysql.  When
functions are nested, often the caller logs the error again, as do users
of this module. Because mysql has a set of own error codes, these are
mapped to system error codes and sometimes these get logged again via
printf "%m", showing some more or less related system error string.

Generally, it unclear, whether the caller or the callee is expected
to log the errors, which leads to the same error logged multiple times
in different forms.

The functions in this module communicate the crafted system error code
in both, errno and as the (negated) return value of the function.
Accesses to errno are expensive (alway a call) and the duplication
leads to complicated code and unclear semantics.

Change the general error handling API for this module:

Mysql error strings, which often contain useful information which is
not encoded in any error indentifier, are captured but not logged by
this module. A new function mx_mysql_error() is provided for the caller
to get the lastest error string.

System error code are returned as before, but errno is not set.

After the change, it is the callers duty to log errors.

    res = mx_mysql_connect(&mx_mysql);
    if (res < 0) {
        fprintf(stderr, mx_mysql_connect: %s\n", mx_mysql_error());
        _exit(1);
    }

This change reduces the object size of mx_mxql by 28%.
  • Loading branch information
donald committed Dec 29, 2023
1 parent 616434d commit 6c28e16
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 235 deletions.
Loading

0 comments on commit 6c28e16

Please sign in to comment.