monitor: convert do_eject() to QError
[qemu/kevin.git] / qerror.c
blob17532b0665ba0d5e789c280046241c691a52a681
1 /*
2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
12 #include "qjson.h"
13 #include "qerror.h"
14 #include "qstring.h"
15 #include "sysemu.h"
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject *obj);
20 static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
25 /**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
29 * %(KEY)
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
33 * Example:
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
40 * "running out of foo: %(foo)%%"
42 static const QErrorStringTable qerror_table[] = {
44 .error_fmt = QERR_COMMAND_NOT_FOUND,
45 .desc = "The command %(name) has not been found",
48 .error_fmt = QERR_DEVICE_ENCRYPTED,
49 .desc = "The %(device) is encrypted",
52 .error_fmt = QERR_DEVICE_LOCKED,
53 .desc = "Device %(device) is locked",
56 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
57 .desc = "The %(device) device has not been activated by the guest",
60 .error_fmt = QERR_DEVICE_NOT_FOUND,
61 .desc = "The %(device) device has not been found",
64 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
65 .desc = "Device %(device) is not removable",
68 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
69 .desc = "Invalid parameter type, expected: %(expected)",
72 .error_fmt = QERR_INVALID_PASSWORD,
73 .desc = "The entered password is invalid",
76 .error_fmt = QERR_JSON_PARSING,
77 .desc = "Invalid JSON syntax",
80 .error_fmt = QERR_KVM_MISSING_CAP,
81 .desc = "Using KVM without %(capability), %(feature) unavailable",
84 .error_fmt = QERR_MISSING_PARAMETER,
85 .desc = "Parameter %(name) is missing",
88 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
89 .desc = "Bad QMP input object",
92 .error_fmt = QERR_UNDEFINED_ERROR,
93 .desc = "An undefined error has ocurred",
98 /**
99 * qerror_new(): Create a new QError
101 * Return strong reference.
103 QError *qerror_new(void)
105 QError *qerr;
107 qerr = qemu_mallocz(sizeof(*qerr));
108 QOBJECT_INIT(qerr, &qerror_type);
110 return qerr;
113 static void qerror_abort(const QError *qerr, const char *fmt, ...)
115 va_list ap;
117 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
118 fprintf(stderr, "qerror: -> ");
120 va_start(ap, fmt);
121 vfprintf(stderr, fmt, ap);
122 va_end(ap);
124 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
125 abort();
128 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
130 QObject *obj;
132 obj = qobject_from_jsonv(fmt, va);
133 if (!obj) {
134 qerror_abort(qerr, "invalid format '%s'", fmt);
136 if (qobject_type(obj) != QTYPE_QDICT) {
137 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
140 qerr->error = qobject_to_qdict(obj);
142 obj = qdict_get(qerr->error, "class");
143 if (!obj) {
144 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
146 if (qobject_type(obj) != QTYPE_QSTRING) {
147 qerror_abort(qerr, "'class' key value should be a QString");
150 obj = qdict_get(qerr->error, "data");
151 if (!obj) {
152 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
154 if (qobject_type(obj) != QTYPE_QDICT) {
155 qerror_abort(qerr, "'data' key value should be a QDICT");
159 static void qerror_set_desc(QError *qerr, const char *fmt)
161 int i;
163 // FIXME: inefficient loop
165 for (i = 0; qerror_table[i].error_fmt; i++) {
166 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
167 qerr->entry = &qerror_table[i];
168 return;
172 qerror_abort(qerr, "error format '%s' not found", fmt);
176 * qerror_from_info(): Create a new QError from error information
178 * The information consists of:
180 * - file the file name of where the error occurred
181 * - linenr the line number of where the error occurred
182 * - func the function name of where the error occurred
183 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
184 * 'data'
185 * - va va_list of all arguments specified by fmt
187 * Return strong reference.
189 QError *qerror_from_info(const char *file, int linenr, const char *func,
190 const char *fmt, va_list *va)
192 QError *qerr;
194 qerr = qerror_new();
195 qerr->linenr = linenr;
196 qerr->file = file;
197 qerr->func = func;
199 if (!fmt) {
200 qerror_abort(qerr, "QDict not specified");
203 qerror_set_data(qerr, fmt, va);
204 qerror_set_desc(qerr, fmt);
206 return qerr;
209 static void parse_error(const QError *qerror, int c)
211 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
214 static const char *append_field(QString *outstr, const QError *qerror,
215 const char *start)
217 QObject *obj;
218 QDict *qdict;
219 QString *key_qs;
220 const char *end, *key;
222 if (*start != '%')
223 parse_error(qerror, '%');
224 start++;
225 if (*start != '(')
226 parse_error(qerror, '(');
227 start++;
229 end = strchr(start, ')');
230 if (!end)
231 parse_error(qerror, ')');
233 key_qs = qstring_from_substr(start, 0, end - start - 1);
234 key = qstring_get_str(key_qs);
236 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
237 obj = qdict_get(qdict, key);
238 if (!obj) {
239 qerror_abort(qerror, "key '%s' not found in QDict", key);
242 switch (qobject_type(obj)) {
243 case QTYPE_QSTRING:
244 qstring_append(outstr, qdict_get_str(qdict, key));
245 break;
246 case QTYPE_QINT:
247 qstring_append_int(outstr, qdict_get_int(qdict, key));
248 break;
249 default:
250 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
253 QDECREF(key_qs);
254 return ++end;
258 * qerror_print(): Print QError data
260 * This function will print the member 'desc' of the specified QError object,
261 * it uses qemu_error() for this, so that the output is routed to the right
262 * place (ie. stderr or Monitor's device).
264 void qerror_print(const QError *qerror)
266 const char *p;
267 QString *qstring;
269 assert(qerror->entry != NULL);
271 qstring = qstring_new();
273 for (p = qerror->entry->desc; *p != '\0';) {
274 if (*p != '%') {
275 qstring_append_chr(qstring, *p++);
276 } else if (*(p + 1) == '%') {
277 qstring_append_chr(qstring, '%');
278 p += 2;
279 } else {
280 p = append_field(qstring, qerror, p);
284 qemu_error("%s\n", qstring_get_str(qstring));
285 QDECREF(qstring);
289 * qobject_to_qerror(): Convert a QObject into a QError
291 QError *qobject_to_qerror(const QObject *obj)
293 if (qobject_type(obj) != QTYPE_QERROR) {
294 return NULL;
297 return container_of(obj, QError, base);
301 * qerror_destroy_obj(): Free all memory allocated by a QError
303 static void qerror_destroy_obj(QObject *obj)
305 QError *qerr;
307 assert(obj != NULL);
308 qerr = qobject_to_qerror(obj);
310 QDECREF(qerr->error);
311 qemu_free(qerr);