Skip to content

Commit

Permalink
Avoid duplication of error messages (#758)
Browse files Browse the repository at this point in the history
# Description
* Improved error handling logics

## Type of change
- [x] Bug fix & code cleanup
- [ ] New feature
- [ ] Documentation update
- [ ] Test update

## Checklist for the reviewer
This checklist should be used as a help for the reviewer.

- [ ] Is the change limited to one issue?
- [ ] Does this PR close the issue?
- [ ] Is the code easy to read and understand?
- [ ] Do all new feature have an accompanying new test?
- [ ] Has the documentation been updated as necessary?
  • Loading branch information
jesper-friis authored Dec 30, 2023
2 parents 747fbaa + af02c4d commit 0bfb554
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cd_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
run: |
sudo apt-get update --fix-missing
sudo apt-get install \
libxml2-dev \
libxslt-dev \
libhdf5-dev \
swig4.0 \
doxygen \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
run: |
sudo apt-get update --fix-missing && \
sudo apt-get install \
libxml2-dev \
libxslt-dev \
libhdf5-dev \
swig4.0 \
doxygen \
Expand Down
59 changes: 43 additions & 16 deletions src/utils/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ int _err_vformat(ErrLevel errlevel, int eval, int errnum, const char *file,
int ignore_new_error = 0;
ErrHandler handler = err_get_handler();
ErrNameConv nameconv = err_get_nameconv();
int call_handler = handler && !tls->err_record->prev;

/* Check warning mode */
if (errlevel == errLevelWarn) {
Expand All @@ -218,6 +217,7 @@ int _err_vformat(ErrLevel errlevel, int eval, int errnum, const char *file,
switch (override) {
case errOverrideAppend:
n = strlen(errmsg);
tls->err_record->pos = n;
n += snprintf(errmsg + n, errsize - n, "%s", err_append_sep);
break;
case errOverrideWarnOld:
Expand Down Expand Up @@ -283,25 +283,25 @@ int _err_vformat(ErrLevel errlevel, int eval, int errnum, const char *file,
tls->err_record->reraise = eval;

/* If we are not within a ErrTry...ErrEnd clause */
if (call_handler) {
if (!tls->err_record->prev) {

/* ...call the error handler */
handler(tls->err_record);
if (handler) handler(tls->err_record);

/* ...check err_abort_mode */
if (errlevel >= errLevelError) {
if (abort_mode == errAbortExit) {
if (!call_handler) handler(tls->err_record);
if (!handler) err_default_handler(tls->err_record);
exit(eval);
} else if (abort_mode >= errAbortAbort) {
if (!call_handler) handler(tls->err_record);
if (!handler) err_default_handler(tls->err_record);
abort();
}
}

/* ...make sure that fatal errors always exit */
if (errlevel >= errLevelFatal) {
if (!call_handler) handler(tls->err_record);
if (!handler) err_default_handler(tls->err_record);
exit(eval);
}
}
Expand Down Expand Up @@ -449,6 +449,7 @@ void err_clear(void)
tls->err_record->eval = 0;
tls->err_record->errnum = 0;
tls->err_record->msg[0] = '\0';
tls->err_record->pos = 0;
tls->err_record->handled = 0;
tls->err_record->reraise = 0;
tls->err_record->state = 0;
Expand Down Expand Up @@ -673,15 +674,42 @@ int err_get_color_coded()
void err_default_handler(const ErrRecord *record)
{
FILE *stream = err_get_stream();
if (err_get_color_coded()) {
//if ((stream == stderr || stream == stdout) && getenv("ERR_COLOR")) {
/* Output the first word of the error message in red and the remaining
of it in magenta. */
int n = strcspn(record->msg, ": ");
fprintf(stream, "\033[31m%.*s\033[35m%s\033[0m\n", n, record->msg,
record->msg+n);
const char *msg = record->msg + record->pos;
char *errmark = (record->pos) ? "" : "** ";
if (record->pos >= ERR_MSGSIZE) return;
if (record->pos) {
int m = strspn(msg, "\n");
int n = strlen(err_append_sep) - m;
fprintf(stream, "%.*s", n, msg+m);
msg += m+n;
}
if (stream && err_get_color_coded()) {
/* Print error message in colour. */
int n;
ThreadLocals *tls = get_tls();
Globals *g = tls->globals;
ErrDebugMode debug_mode = err_get_debug_mode();
if (g->err_prefix && *g->err_prefix) {
n = strlen(g->err_prefix) + 2;
if (!record->pos) fprintf(stream, "\033[02;31m%.*s", n, msg);
msg += n;
}
if (debug_mode >= 1) {
n = strcspn(msg, ":") + 1;
n += (msg[0] == '(') ? 1 : strcspn(msg+n, ":") + 2;
fprintf(stream, "\033[00;34m%.*s", n, msg);
msg += n;
}
if (debug_mode >= 2) {
n = strcspn(msg, ":") + 2;
fprintf(stream, "\033[02;32m%.*s", n, msg);
msg += n;
}
n = strcspn(msg, ": ");
fprintf(stream, "\033[00;31m%.*s\033[02;35m%s\033[0m\n", n, msg, msg+n);
} else if (stream) {
fprintf(stream, "** %s\n", record->msg);
/* Print error message with error marker prepended. */
fprintf(stream, "%s%s\n", errmark, msg);
}
}

Expand Down Expand Up @@ -756,7 +784,6 @@ void _err_link_record(ErrRecord *record)
void _err_unlink_record(ErrRecord *record)
{
ThreadLocals *tls = get_tls();
Globals *g = tls->globals;
assert(record == tls->err_record);
assert(tls->err_record->prev);

Expand Down Expand Up @@ -800,7 +827,7 @@ void _err_unlink_record(ErrRecord *record)

if (!tls->err_record->prev) {
ErrHandler handler = err_get_handler();
if (handler) g->err_handler(tls->err_record);
if (handler) handler(tls->err_record);
}

if ((abort_mode && record->level >= errLevelError) ||
Expand Down
1 change: 1 addition & 0 deletions src/utils/err.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ typedef struct ErrRecord {
int eval; /*!< @brief Error value. */
int errnum; /*!< @brief System error number. */
char msg[ERR_MSGSIZE]; /*!< @brief Error message. */
int pos; /*!< @brief Position of new appended error message. */
int handled; /*!< @brief Whether the error has been handled. */
int reraise; /*!< @brief Error value to reraise. */
ErrTryState state; /*!< @brief Where we are in ErrTry.. ErrEnd. */
Expand Down

0 comments on commit 0bfb554

Please sign in to comment.