Today, the error message is only constructed when it's used. This commit changes that to construct the error message when the error object is built (ie. when the error is reported).
This eliminates the need of storing a pointer to qerror_table[], which will be dropped soon, and also simplifies the code. Signed-off-by: Luiz Capitulino <lcapitul...@redhat.com> --- qerror.c | 34 +++++++--------------------------- qerror.h | 2 +- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/qerror.c b/qerror.c index 7db28fc..89def8d 100644 --- a/qerror.c +++ b/qerror.c @@ -81,22 +81,6 @@ out_free: return NULL; } -static const QErrorStringTable *error_get_desc(const char *fmt) -{ - int i; - - // FIXME: inefficient loop - - for (i = 0; qerror_table[i].error_fmt; i++) { - if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { - return &qerror_table[i]; - } - } - - fprintf(stderr, "error format '%s' not found\n", fmt); - return NULL; -} - static QDict *build_error_no_arg(const char *fmt) { return qobject_to_qdict(qobject_from_jsonv(fmt, NULL)); @@ -119,8 +103,8 @@ static QError *qerror_from_info(const char *fmt, va_list *va) goto bad_err; } - qerr->entry = error_get_desc(fmt); - if (!qerr->entry) { + qerr->err_msg = qerror_format(fmt, qerr->error); + if (!qerr->err_msg) { QDECREF(qerr->error); goto bad_err; } @@ -129,7 +113,7 @@ static QError *qerror_from_info(const char *fmt, va_list *va) bad_err: qerr->error = build_error_no_arg(QERR_UNDEFINED_ERROR); - qerr->entry = error_get_desc(QERR_UNDEFINED_ERROR); + qerr->err_msg = qerror_format(QERR_UNDEFINED_ERROR, qerr->error); return qerr; } @@ -233,7 +217,7 @@ char *qerror_format(const char *fmt, QDict *error) */ QString *qerror_human(const QError *qerror) { - return qerror_format_desc(qerror->error, qerror->entry); + return qstring_from_str(qerror->err_msg); } /** @@ -280,19 +264,14 @@ struct Error void qerror_report_err(Error *err) { QError *qerr; - int i; qerr = qerror_new(); loc_save(&qerr->loc); QINCREF(err->obj); qerr->error = err->obj; - for (i = 0; qerror_table[i].error_fmt; i++) { - if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) { - qerr->entry = &qerror_table[i]; - break; - } - } + qerr->err_msg = qerror_format(err->fmt, qerr->error); + /* FIXME: should report UndefinedError on error */ if (monitor_cur_is_qmp()) { monitor_set_error(cur_mon, qerr); @@ -333,5 +312,6 @@ static void qerror_destroy_obj(QObject *obj) qerr = qobject_to_qerror(obj); QDECREF(qerr->error); + g_free(qerr->err_msg); g_free(qerr); } diff --git a/qerror.h b/qerror.h index 6bf941b..16401ff 100644 --- a/qerror.h +++ b/qerror.h @@ -28,7 +28,7 @@ typedef struct QError { QObject_HEAD; QDict *error; Location loc; - const QErrorStringTable *entry; + char *err_msg; } QError; QString *qerror_human(const QError *qerror); -- 1.7.11.2.249.g31c7954.dirty