diff options
author | Markus Armbruster <armbru@redhat.com> | 2018-07-03 10:53:48 +0200 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2018-07-03 23:18:56 +0200 |
commit | cee32796cadc9510ee00f029a933009df7a28ae2 (patch) | |
tree | 1fb09ff3405d4ca4b75afc6304047f7376dd1ece | |
parent | a193352ff9c7cd2cd07846118bc49921d0f53af8 (diff) | |
download | qemu-cee32796cadc9510ee00f029a933009df7a28ae2.zip qemu-cee32796cadc9510ee00f029a933009df7a28ae2.tar.gz qemu-cee32796cadc9510ee00f029a933009df7a28ae2.tar.bz2 |
qmp: De-duplicate error response building
All callers of qmp_build_error_object() duplicate the code to wrap it
in a response object. Replace it by qmp_error_response() that
captures the duplicated code, including error_free().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180703085358.13941-23-armbru@redhat.com>
-rw-r--r-- | include/qapi/qmp/dispatch.h | 2 | ||||
-rw-r--r-- | monitor.c | 7 | ||||
-rw-r--r-- | qapi/qmp-dispatch.c | 20 | ||||
-rw-r--r-- | qga/main.c | 8 |
4 files changed, 15 insertions, 22 deletions
diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 514bfc4..a53e11c 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -47,7 +47,7 @@ void qmp_enable_command(QmpCommandList *cmds, const char *name); bool qmp_command_is_enabled(const QmpCommand *cmd); const char *qmp_command_name(const QmpCommand *cmd); bool qmp_has_success_response(const QmpCommand *cmd); -QObject *qmp_build_error_object(Error *err); +QDict *qmp_error_response(Error *err); QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request, bool allow_oob); bool qmp_is_oob(QDict *dict); @@ -4107,14 +4107,9 @@ static int monitor_can_read(void *opaque) static void monitor_qmp_respond(Monitor *mon, QObject *rsp, Error *err, QObject *id) { - QDict *qdict = NULL; - if (err) { assert(!rsp); - qdict = qdict_new(); - qdict_put_obj(qdict, "error", qmp_build_error_object(err)); - error_free(err); - rsp = QOBJECT(qdict); + rsp = QOBJECT(qmp_error_response(err)); } if (rsp) { diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 6d78f3e..c85748a 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -141,11 +141,15 @@ static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request, return ret; } -QObject *qmp_build_error_object(Error *err) +QDict *qmp_error_response(Error *err) { - return qobject_from_jsonf("{ 'class': %s, 'desc': %s }", - QapiErrorClass_str(error_get_class(err)), - error_get_pretty(err)); + QDict *rsp; + + rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }", + QapiErrorClass_str(error_get_class(err)), + error_get_pretty(err)); + error_free(err); + return rsp; } /* @@ -166,15 +170,13 @@ QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request, ret = do_qmp_dispatch(cmds, request, allow_oob, &err); - rsp = qdict_new(); if (err) { - qdict_put_obj(rsp, "error", qmp_build_error_object(err)); - error_free(err); + rsp = qmp_error_response(err); } else if (ret) { + rsp = qdict_new(); qdict_put_obj(rsp, "return", ret); } else { - qobject_unref(rsp); - return NULL; + rsp = NULL; } return QOBJECT(rsp); @@ -610,15 +610,13 @@ static void process_event(JSONMessageParser *parser, GQueue *tokens) qdict = qobject_to(QDict, json_parser_parse_err(tokens, NULL, &err)); if (err || !qdict) { qobject_unref(qdict); - qdict = qdict_new(); if (!err) { g_warning("failed to parse event: unknown error"); error_setg(&err, QERR_JSON_PARSING); } else { g_warning("failed to parse event: %s", error_get_pretty(err)); } - qdict_put_obj(qdict, "error", qmp_build_error_object(err)); - error_free(err); + qdict = qmp_error_response(err); } /* handle host->guest commands */ @@ -627,11 +625,9 @@ static void process_event(JSONMessageParser *parser, GQueue *tokens) } else { if (!qdict_haskey(qdict, "error")) { qobject_unref(qdict); - qdict = qdict_new(); g_warning("unrecognized payload format"); error_setg(&err, QERR_UNSUPPORTED); - qdict_put_obj(qdict, "error", qmp_build_error_object(err)); - error_free(err); + qdict = qmp_error_response(err); } ret = send_response(s, QOBJECT(qdict)); if (ret < 0) { |