QError: Introduce qerror_format_desc()

Refactor non-QError-specific bits out of qerror_human() into general
function that can be used by the error_get_pretty() analogue in the
new error-propagation framework.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Luiz Capitulino 2011-06-01 12:14:47 -05:00 committed by Anthony Liguori
parent 23bf93b215
commit a12eeaaa4f
1 changed files with 25 additions and 19 deletions

View File

@ -326,12 +326,14 @@ QError *qerror_from_info(const char *file, int linenr, const char *func,
return qerr; return qerr;
} }
static void parse_error(const QError *qerror, int c) static void parse_error(const QErrorStringTable *entry, int c)
{ {
qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc); fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
abort();
} }
static const char *append_field(QString *outstr, const QError *qerror, static const char *append_field(QDict *error, QString *outstr,
const QErrorStringTable *entry,
const char *start) const char *start)
{ {
QObject *obj; QObject *obj;
@ -340,23 +342,23 @@ static const char *append_field(QString *outstr, const QError *qerror,
const char *end, *key; const char *end, *key;
if (*start != '%') if (*start != '%')
parse_error(qerror, '%'); parse_error(entry, '%');
start++; start++;
if (*start != '(') if (*start != '(')
parse_error(qerror, '('); parse_error(entry, '(');
start++; start++;
end = strchr(start, ')'); end = strchr(start, ')');
if (!end) if (!end)
parse_error(qerror, ')'); parse_error(entry, ')');
key_qs = qstring_from_substr(start, 0, end - start - 1); key_qs = qstring_from_substr(start, 0, end - start - 1);
key = qstring_get_str(key_qs); key = qstring_get_str(key_qs);
qdict = qobject_to_qdict(qdict_get(qerror->error, "data")); qdict = qobject_to_qdict(qdict_get(error, "data"));
obj = qdict_get(qdict, key); obj = qdict_get(qdict, key);
if (!obj) { if (!obj) {
qerror_abort(qerror, "key '%s' not found in QDict", key); abort();
} }
switch (qobject_type(obj)) { switch (qobject_type(obj)) {
@ -367,41 +369,45 @@ static const char *append_field(QString *outstr, const QError *qerror,
qstring_append_int(outstr, qdict_get_int(qdict, key)); qstring_append_int(outstr, qdict_get_int(qdict, key));
break; break;
default: default:
qerror_abort(qerror, "invalid type '%c'", qobject_type(obj)); abort();
} }
QDECREF(key_qs); QDECREF(key_qs);
return ++end; return ++end;
} }
/** static QString *qerror_format_desc(QDict *error,
* qerror_human(): Format QError data into human-readable string. const QErrorStringTable *entry)
*
* Formats according to member 'desc' of the specified QError object.
*/
QString *qerror_human(const QError *qerror)
{ {
const char *p;
QString *qstring; QString *qstring;
const char *p;
assert(qerror->entry != NULL); assert(entry != NULL);
qstring = qstring_new(); qstring = qstring_new();
for (p = qerror->entry->desc; *p != '\0';) { for (p = entry->desc; *p != '\0';) {
if (*p != '%') { if (*p != '%') {
qstring_append_chr(qstring, *p++); qstring_append_chr(qstring, *p++);
} else if (*(p + 1) == '%') { } else if (*(p + 1) == '%') {
qstring_append_chr(qstring, '%'); qstring_append_chr(qstring, '%');
p += 2; p += 2;
} else { } else {
p = append_field(qstring, qerror, p); p = append_field(error, qstring, entry, p);
} }
} }
return qstring; return qstring;
} }
/**
* qerror_human(): Format QError data into human-readable string.
*/
QString *qerror_human(const QError *qerror)
{
return qerror_format_desc(qerror->error, qerror->entry);
}
/** /**
* qerror_print(): Print QError data * qerror_print(): Print QError data
* *